foreach php nested array [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How foreach this array to < a href="path-to-jpg-file">name< /a>
Array
(
[0] => Array
(
[0] => 01-siena-rosso-new.jpg
[1] => Siena Rosso
)
[1] => Array
(
[0] => 02-siena-noce-new.jpg
[1] => Siena Noce
)
[2] => Array
(
[0] => 03-zloty-dab.jpg
[1] => Złoty Dąb
)
)

Try loop like this:-
foreach($array as $value) {
echo ''.$value[1].'';
}
Working Demo

Expanding on Roopendras, you just need preg_replace:
foreach($array as $value) {
echo ''.$value[1].'';
}

Related

converting array of objects to simple array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an array of objects, each object consists of an id and an organisation. It looks like this
Array (
[0] => stdClass Object (
[id] => 2 [organisation] => org1
)
[1] => stdClass Object (
[id] => 4 [organisation] => org2
)
[2] => stdClass Object (
[id] => 1 [organisation] => org3
)
)
I need to convert it into a simple associative array ([id]=>organisation,...) so the above example would look like this
Array (
[2] => org1
[4] => org2
[1] => org3
)
Greatful for any thoughts
Loop through it using a foreach statement and append it to another array.
$finished = [];
foreach($array as $arr) {
$finished[$arr->id] = $arr->organisation;
}
$result = array();
foreach($array as $arr) {
$result[$arr->id] = $arr->organisation;
}
echo "<pre>";print_r($result);

Removed nested array php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an array which is
Array
(
[0] => Array
(
[city_id] => 16
[city_name] => San Diego
[state_abbreviation] => CA
[city_lat] => 32.715328
[city_long] => -117.157257
[city_image] => sandiego_ca.jpg
)
)
i just want to show
Array
(
[city_id] => 16
[city_name] => San Diego
[state_abbreviation] => CA
[city_lat] => 32.715328
[city_long] => -117.157257
[city_image] => sandiego_ca.jpg
)
Basically removing the [0] nest so i could parse it better
not php array can do like this . just use a tmp variable.
$arr = $this_arr[0];
That's it .

How to compare arrays and objects to find a match PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My first question is, how do I search values of my array inside the objects array [words] and return object if match is found?
How do I search for a better match? In the example below, second object is a better match with 2 words in common, rather than first with only 1 match.
Array
(
[0] => blue
[1] => green
[2] => love
[3] => sandro
)
stdClass Object
(
[1] => stdClass Object
(
[words] => Array
(
[0] => green
[1] => blue
)
[html] => html+img+link+code
)
[2] => stdClass Object
(
[words] => Array
(
[0] => love
[1] => sex
[2] => blue
)
[html] => html+img+link+code
)
)
Code I tried:
foreach ($ads_arr as $ad) {
print_r(array_intersect($ad->words,$words_arr));
}
You can use a forloop for your case, but you should consider defining real php class (not stdClass ) and implement some methods to help you.
foreach($main_std as $id => $sub_std){
$count_match[$id] = 0;
// now, check for each objects
// you can use an other loop with in_array, array_intersect
// or any other way
foreach($the_array as $word_search)
{
// for each word you're looking for, add +1
if (in_array($word_search, $sub_std->words))
$count_match[$id] ++;
}
}
// here, $count_match is an array you can sort by best match or whatever you want
Try out array_intersect() :
$output = array_intersect($array1, $array2);
print_r($output);

Get values form multidimensional array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Right now, this block of code displays VLANs. I'm trying to display only the names(TLSoIP_JB1, TLSoIP_JB2, TLSoIP_JB3)
Array (
[0] => Array ( [0] => TLSoIP_JB1 [1] => 1 )
[1] => Array ( [0] => TLSoIP_JB2 [1] => 2 )
[2] => Array ( [0] => TLSoIP_JB3 [1] => 3 )
)
Regards Philipp
You can try:
foreach ($array as $key => $inner_array) {
echo $inner_array[0];
}
Or you can store the desired values in a separate array and print it afterwards:
$new_array = array();
foreach ($array as $key => $inner_array) {
$new_array[] = $inner_array[0];
}
print_r($new_array);

how to print values if we have an assoc array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working in codeigniter.
Structure of my array is
Array
(
[0] => stdClass Object
(
[0] => 15
[1] => 14
[2] => 0
[3] => 0
[4] => 0
)
[1] => stdClass Object
(
[0] => 15
[1] => 14
[2] => 0
[3] => 0
[4] => 0
)
)
I want to print the value of arrays.
As soon as they are not arrays but objects, you need to use
$arr[0]->{0}
form.
Use foreach for access all
foreach ($array as $values)
{
foreach ($values as $value)
{
echo $value;
}
}
Or direct access:
$array[0]->{1};
$array[0]->{2};
$array[1]->{5};
etc..
Usage:
$array[ARRAY INDEX]->{OBJECT INDEX};
Suppose your array is in the variable $arr. Then use it:
$i = 0;
foreach($arr as $array){
echo $array[$i];
$i++;
}
OR you can do this:
echo $arr[1]->0; //and so on
You can get data using
$arr[0]->{0}
OR
$arr[0]->{'0'}
stdClass is php's generic empty class, kind of like Object in Java or object in Python. It is useful for anonymous objects, dynamic properties, etc.
So its not array its an object. you will have to use object operator (->)
check below post
stdClass Object foreach PHP

Categories