0

I have an array in php looks like this.

Array
(
    [0] => Array
        (
            [source_language] => English
            [target_language] => German
            [document_name] => 1439441727_plugin (copy).docx

            [title] => plugin (copy).docx
            [product_group_name] => Article Rewrite
            [id] => 1
        )

    [1] => Array
        (
            [source_language] => English
            [target_language] => German
            [document_name] => 1439536258_plugin (copy).docx

            [title] => plugin (copy).docx
            [product_group_name] => Article Rewrite
            [id] => 10
        )

)

I want to get the value from array. For that I am trying like this

$my_array = $array_data;
print_r($my_array);
//Here it showed the array like the above

foreach($my_array as $array_key) {
    foreach( $array_key as $array_value) {
        var_dump($array_value);
    }
}

But here I have to make two loops to get data. So is there any kind of other way in which I can get the data in more standard way? Any help and suggestions will be really appreciable. Thanks

5
  • 3
    You can use 1 loop: foreach($my_array as $array_key=>$arrayvalue) { Thats pretty standard Commented Aug 18, 2015 at 14:10
  • $my_array[1]['product_group_name'] -- no other way Commented Aug 18, 2015 at 14:11
  • @VasiliyVanchuk I have more then 100 of array then what about $my_array[1]? Commented Aug 18, 2015 at 14:13
  • Whats the standard that you might be looking for Commented Aug 18, 2015 at 14:15
  • what exactly are you trying to do with your results? this will give us a better way of coming up with the correct answer. Commented Aug 18, 2015 at 14:25

2 Answers 2

3

I believe youre not understanding how a foreach loop works.

there are two ways you can use foreach:

  • Like this: foreach($arrs as $arr){}

  • Or like this: foreach($arrs as $key => $arr ){}

The way you have it right now, you are accessing the line values rather than your array block.

What you need to do is:

foreach($my_array as $arr) {
    var_dump($arr);
}

This will result in :

[source_language] => English
[target_language] => German
[document_name] => 1439536258_plugin (copy).docx
[title] => plugin (copy).docx
[product_group_name] => Article Rewrite
[id] => 10

which you can then select like this:

foreach($my_array as $arr) {
    var_dump($arr['id']);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Look at array_walk_recursive http://php.net/manual/en/function.array-walk-recursive.php

(PHP 5) array_walk_recursive — Apply a user function recursively to every member of an array

It implements your nested loops

array_walk_recursive($my_array, function($array_value) {
  var_dump($array_value);
});

4 Comments

While this is indeed a solution to avoid nested foreach loops, I believe the data shown by the OP does not really correspond to this pattern. Here we have more of a case of "array of objects", where there is no reason to loop over the values, since we would then lose the association between keys and values in the process.
Just implement for your objects Iterator interface php.net/manual/en/class.iterator.php -- You shouldn't use only array -- it just must be iterable
Either the objects' fields are known in advance as a pre-established and documented protocol, in which case making those objects iterable makes little to no sense, either the OP is trying to parse random data where this approach makes as little sense, seeing that these arrays are already iterable.
The question was about arrays -- answer about arrays too )) I'm not sure that without full description of task I'm ready discuss implementation

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.