0

I have decided to set up an array to differentiate certain values. If I look to find a certain value in the array, I'd like to use the rest of the data within this value I have searched:

Array
(
    [0] => Array
        (
            [id] => 123
            [0] => Array
                (
                    [name] => Burger Joint
                    [pluralName] => Burger Joints
                    [shortName] => Burgers
                )

        )

    [1] => Array
        (
            [id] => 617
            [0] => Array
                (
                    [name] => reeeJoint
                    [pluralName] => reeeJoints
                    [shortName] => reee
                )

        )

    [2] => Array
        (
            [id] => 12355
            [0] => Array
                (
                    [name] => LeftJoint
                    [pluralName] => LeftJoints
                    [shortName] => Left
                )

        )
    [4] => Array
        (
            [id] => 526
            [0] => Array
                (
                    [name] => asdfJoint
                    [pluralName] => asdfJoints
                    [shortName] => asdf
                )

        )
)

I'd like to do some kind of search where if there's a value with 123, it will only get a new array with everything in the 123 id array (in this case is the first one).

What is the best practice in finding this?

Thanks!

Edit:

Based on some comments, I was able to come up with a new way of setting my array. However I'm still wondering the same question which way to tackle showing if value exists:

foreach($values as $value)
{
 ...
   $categoriesExtract[$id] = array($category_stringArray2Sugg);
}

gives me this array:

Array
(
    [123] => Array
        (
            [0] => Array
                (
                    [name] => Burger Joint
                    [pluralName] => Burger Joints
                    [shortName] => Burgers
                )

        )

    [617] => Array
        (
            [0] => Array
                (
                    [name] => reeeJoint
                    [pluralName] => reeeJoints
                    [shortName] => reee
                )

        )

    [12355] => Array
        (
            [0] => Array
                (
                    [name] => LeftJoint
                    [pluralName] => LeftJoints
                    [shortName] => Left
                )

        )
    [526] => Array
        (
            [0] => Array
                (
                    [name] => asdfJoint
                    [pluralName] => asdfJoints
                    [shortName] => asdf
                )

        )
)
5
  • You should be using objects - not arrays. If you turned this into a single array of objects, which had an array for a member variable, it would be alot easier to understand. Commented Feb 22, 2013 at 4:27
  • what output do you want. can you show here? Commented Feb 22, 2013 at 4:27
  • @NicholasPickering I updated my answer to show how I got my array. Can explain how I would be able to do it in objects? Commented Feb 22, 2013 at 4:40
  • @Roopendra I'd like to just show an array of name,pluralName,shortName Just getting those data so I can echo out in json format Commented Feb 22, 2013 at 4:41
  • @andrewliu , Check my answer hope it will help you Commented Feb 22, 2013 at 4:58

5 Answers 5

1

Why don't you use the id as the key, then pull out the key?

$array = Array
(
    [123] => Array
        (
            [name] => Burger Joint
            [pluralName] => Burger Joints
            [shortName] => Burgers
        )

    [617] => Array
        (
            [name] => reeeJoint
            [pluralName] => reeeJoints
            [shortName] => reee
        )
)


$new_array = $array[123];

You had 2 items with the ID 123, which would mean this situation wouldn't work, but in your question you indicated you wanted the first value returned. Is the double up on the ID an error, or is it something that can legitimately happen because this will mean this won't be a suitable way of doing it.

2
  • Sorry, that 123 was a typo. each array is supposed to have a unique id. I've updated my array to show something similar Commented Feb 22, 2013 at 4:39
  • This actually lead me to the answer i was looking for. thanks Commented Feb 22, 2013 at 5:07
1

Use this code. It will create new array following the match.

 $arr = array();
 for($i = 0;$i<count($array);$i++)
 {
     if($array[$i][id] == 123)
     {
          $arr[] = $array[$i][0];
     }
 }
 print_r($arr);
0

You can simple do like this:

$array = array(0 => 
                  array('id' => 123,
                        0 => array('name' => 'Burger Joint',
                                   'pluralName' => 'Burger Joints',
                                   'shortName' => 'Burgers')
                                   )
                );

$new_array =  array();
foreach($array as $value){
   $new_array[$value['id']][] = $value[0];
}

Output will be

Array
(
    [123] => Array
        (
            [0] => Array
                (
                    [name] => Burger Joint
                    [pluralName] => Burger Joints
                    [shortName] => Burgers
                )

        )

)

Cheeers !!!!!

Check working demo here working demo link

0

Try this solution : Here change the values in $find array to search for ids

$array  = Array(Array("id" => 123,0 => Array("name" => "Burger Joint","pluralName" => "Burger Joints","shortName" => "Burgers")),
                Array("id" => 617,0 => Array("name" => "reeeJoint","pluralName" => "reeeJoints","shortName" => "reee")),
                Array("id" => 12355,0 => Array("name" => "LeftJoint","pluralName" => "LeftJoints","shortName" => "Left")),
                Array("id" => 526,0 => Array("name" => "asdfJoint","pluralName" => "asdfJoints","shortName" => "asdf"))
                );
$find   = array(123,526); /// Get all the array with this id

$result = array();
foreach($array as $vals){
    if(in_array($vals['id'],$find)){
        $result[]  = $vals;
    }
}

echo "<pre>";
print_r($result);
0

I think I just made something easy more complicated...

Because of the suggestions, I was just able to get the array by doing this

$array(123); => this will just get me the array that has the value 123...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.