-2

I have an array like this:

Array(
    [0]=>Array([uploaderName]=>x[uploadedImageName]=>k6gIjfO[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [1]=>Array([uploaderName]=>x[uploadedImageName]=>byUTyJo[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [2]=>Array([uploaderName]=>x[uploadedImageName]=>oSVEnNk[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [3]=>Array([uploaderName]=>x[uploadedImageName]=>Dj7GRYS[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [4]=>Array([uploaderName]=>x[uploadedImageName]=>upsb8IC[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [5]=>Array([uploaderName]=>x[uploadedImageName]=>YoEEzGi[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [6]=>Array([uploaderName]=>x[uploadedImageName]=>st3dLNs[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [7]=>Array([uploaderName]=>x[uploadedImageName]=>LBNpiIG[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [8]=>Array([uploaderName]=>x[uploadedImageName]=>mFYDmBG[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [9]=>Array([uploaderName]=>x[uploadedImageName]=>z03kSx1[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
)

I want to get any image's data from this array.

Example: When user shows uploadedImageName == jCPjeWv, I wan't to get who is it's uploader.

1

3 Answers 3

1

Simple, just use foreach

$arr = array(/* content here */);

foreach($arr as $value){
    if($value['uploadedImageName'] == 'jCPjeWv'){
        echo $value['uploaderName'];
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just for fun, here's another way:

echo array_column($array, 'uploaderName', 'uploadedImageName')['jCPjeWv'];
  • Get an array of uploaderName with the index set to uploadedImageName
  • Access the index using the uploadedImageName 'jCPjeWv'

Obviously to do it multiple times you would want to actually create a new array:

$images = array_column($array, 'uploaderName', 'uploadedImageName');
echo $images['jCPjeWv'];

If you want to access the other values as well, then use null instead of uploaderName:

$images = array_column($array, null, 'uploadedImageName');
echo $images['jCPjeWv']['uploaderName'];
echo $images['jCPjeWv']['uploaderIp'];

NOTE: These ways only work if the uploadedImageName is unique.

3 Comments

Good trick. Doesn't it take too much resources from the server (Memory/Processing)?
@CarlosCarucce: Doubt it, but added a reusable example.
To clarify for researchers, using a break -able foreach() will likely perform better than functional-style approaches (array_column() and array_filter()) because foreach() has the potential to stop iterating as soon as it finds a match. Conversely, functional iterators cannot stop when a match is found -- they will unconditionally iterarate the entire array.
0

A foreach is possible, but I think it's important to learn the array functions as well so I'm just going to put this example here.

$uploadedImageName = 'jCPjeWv';
$filtered = array_filter($array, function($value) use ($uploadedImageName) {
   return ($value['uploadedImageName'] == $uploadedImageName);
});

This will return a $filtered with the other arrays removed since their $value['uploadedImageName'] will not equal $uploadedImageName.

For more info check out the http://php.net/manual/en/function.array-filter.php manual.

2 Comments

To clarify for researchers, using a break -able foreach() will likely perform better than functional-style approaches (array_column() and array_filter()) because foreach() has the potential to stop iterating as soon as it finds a match. Conversely, functional iterators cannot stop when a match is found -- they will unconditionally iterarate the entire array.
Yes that is true, most languages have a concept of a "first" or "single" match filter, but it's been a while since I've used PHP, I can't recall if that type of function exists natively.

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.