I need to fetch data from the array that I got through
print_r($result);
The array I got is
Array
(
[0] => Array
(
[id] =>
[Location] => Array
(
[img] => 177223
[name] =>
)
[Max] =>
[Total] =>
[Description] => Array
(
[Pre] =>
[Updated] =>
[Program] => Array
(
[Schedule] =>
)
)
[Staff] => Array
(
[FirstName] =>
)
)
)
I used this code
if (!empty($result))
{
foreach ($result as $res)
{
$Max = $res['Max'];
echo $Max;
echo "<br>";
if(isset($res['Location']))
{
foreach($res['Location'] as $loc)
{
$img= $loc['img'];
echo $img;
echo "<br>";
}
}
}
}
I am getting correct value for the first array (I.e Max etc) but not for Location, Description and Staff, can anyone correct my code
Location is not an array of arrays.
It is just an associative array.
if (!empty($result)) {
foreach ($result as $res) {
$Max = $res['Max'];
echo $Max;
echo "<br>";
if(isset($res['Location'])) {
$img= $res['Location']['img'];
echo $img;
echo "<br>";
}
}
}
You dont need to foreach through location, just access it's elements directly:
if(isset($res['Location']))
{
$img= $res['Location']['img'];
echo $img;
echo "<br>";
}
Or somethig like that.
Related
I have this stdclass object in array
Array ( [0] => stdClass Object ( [file_id] => 6 [file_name] => 1ofdays.wav ) [1] => stdClass Object ( [file_id] => 7 [file_name] => abcd.mp3 ) )
I want to convert this stdclass object array into associative arrays such as
$name_array=([text]=>1ofdays.wav,[text]=>abcd.mp3);
$id_array=([value]=>6,[value]=>7);
I tried achieving it by first flattening the array using this function
public function array_flatten($mArray) {
$sArray = array();
foreach ($mArray as $row) {
if ( !(is_array($row)) ) {
if($sArray[] = $row){
}
} else {
$sArray = array_merge($sArray,$this->array_flatten($row));
}
}
return $sArray;
}
This function gave me result
Array ( [0] => 6 [1] => 1ofdays.wav [2] => 7 [3] => abcd.mp3 )
then I created two arrays even() and odd()
and took elements from odd and even indexes of the and pushed them in their respected arrays
which resulted in
even( [0] => 6 [1] => 7 )
odd( [0] => 1ofdays.wav [1] => abcd.mp3 )
now i want to put elements of even array into
Id_array =('value'=>6,'value'=>7)
and
name_array=('text'=>1ofdays.wav,'text'=>'abcd.mp3')
Hope It is same as you want
<?php
class stdClass1 {
public $file_id;
public $file_name;
public function setName($n, $sn) {
$this->file_id = $n;
$this->file_name = $sn;
}
}
$exaObj = new stdClass1;
$exaObj2 = new stdClass1;
$exaObj->setName('6', '1ofdays.wav');
$exaObj2->setName('7', 'abcd.mp3');
$arr = array($exaObj, $exaObj2);
foreach ($arr as $k => $v) {
print_r($v);
echo "<br>";
foreach ($v as $key => $value) {
$$key[] = $value;
}
}
echo "<pre>";
print_r($file_id);
echo "</pre>";
echo "<pre>";
print_r($file_name);
echo "</pre>";
echo "Second METHOD<br><br>";
foreach ($arr as $k => $v) {
$nameArr[] = $v->file_name;
$idArr[] = $v->file_id;
}
echo "<pre>";
print_r($idArr);
echo "</pre>";
echo "<pre>";
print_r($nameArr);
echo "</pre>";
?>
I have this array:
Array
(
[datas] => Array
(
[General] => Array
(
[0] => Array
(
[id] => logo
[size] => 10
)
)
[Rooms] => Array
(
[0] => Array
(
[id] => room_1
[size] => 8
)
[1] => Array
(
[id] => room_2
[size] => 8
)
[2] => Array
(
[id] => room_3
[size] => 8
)
)
)
)
I need to update it when I receive some info like this:
$key = 'room_3';
$toChange = '9';
So in my array, I want to change the size of room_3.
I will always edit the same element (i.e. size).
What I tried:
// Function to communicate with the array
getDatas($array, 'room_3', '9');
function getDatas($datas, $got, $to_find) {
foreach ($datas as $d) {
if (array_search($got, $d)) {
if (in_array($to_find, array_keys($d))) {
return trim($d[$to_find]);
}
}
}
}
But it does not work...
Could you please help me ?
Thanks.
function getDatas($datas, $got, $to_find) {
foreach($datas['datas'] as $key => $rows) {
foreach($rows as $number => $row) {
if($row['id'] == $got) {
// u can return new value
return $row['size'];
// or you can change it and return update array
$datas['dates'][$key][$number]['size'] = $to_find; // it should be sth like $new value
return $datas;
}
}
}
}
function changeRoomSize (&$datas, $roomID, $newSize ){
//assuming that you have provided valid data in $datas array
foreach($datas['datas']['Rooms'] as &$room){
if($room['id'] == $roomID){
$room['size'] = $newSize;
break;//you can add break to stop looping after the room size is changed
}
}
}
//--- > define here your array with data
//and then call this function
changeRoomSize($data,"room_3",9);
//print the results
var_dump($data);
It's a 3-dimensional array, if you want to change the value, do like this:
$key = 'room_3';
$toChange = '9';
$array['datas'] = getRooms($array['datas'], $key, $toChange);
function getRooms($rooms, $key, $toChange) {
foreach($rooms as $k1=>$v1) foreach ($v1 as $k2=>$v2) {
if ($v2['id'] == $key)) {
$rooms[$k1][$k2]['size'] = $toChange;
}
}
return $rooms;
}
print_r($array);
I would like to get the values of id and name inside this array.
Array
(
[data] => Array
(
[0] => Array
(
[id] => 238345159549706
[members] => Array
(
[data] => Array
(
[0] => Array
(
[id] => 100001130889528
[name] => Sy Cheeze
)
[1] => Array
(
[id] => 100002616426665
[name] => Chun Jenny
)
.......
I've tried using this foreach.
foreach ($acquaintances as $acquaintance)
{
foreach ($acquaintance as $acquaint)
{
$acqID = $acquaint['id'];
$acqName = $acquaint['name'];
echo $acqName;
}
}
but nothing will be displayed. What would I do with my code? Any idea and suggestions please. Thank you!
$array = array
(
array("bla",22,18),
array("blaa",15,13),
array("blaaa",5,2),
array("blaaaa",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$array[$row][$col]."</li>";
}
echo "</ul>";
}
You can also access the indices directly in your foreach loop. Like this:
foreach($acquaintances['data'] as $acquaintance) {
foreach($acquaintance['members']['data'] as $acquaint) {
$acqID = $acquaint['id'];
$acqName = $acquaint['name'];
echo $acqName . '<br/>';
}
}
I have a following array
Array
(
[0] => Array
(
[0] =>
)
[1] => Array
(
[0] => flatrate
[1] => flatrate3
[2] => freeshipping
)
[2] => Array
(
[0] => flatrate
[1] => flatrate2
[2] => flatrate3
[3] => flatrate4
[4] => freeshipping
)
)
Now, i need to match some element in this like
if freeshipping in above array . it should echo yes else no.
Please suggest, how can i do this.
for($i = 0; count($array); $i++){
if(in_array($string, $array[$i]) echo true;
}
you can do simple code like that :
foreach ($array as $key) {
foreach ($key as $value) {
if($value == "your condition")
{
echo 'yes';
}else{
echo 'no';
}
}
}
try it.
Create A function
function checkfreeShipping($arrData)
{
foreach($arrData as $data){
if(in_array('freeshipping',$data))
return true;
}
}
Call defined function to check Shipping -
if(checkfreeShipping($cartArr)){
echo "free Shipping";
}
else{
echo "Paid Shipping";
}
Below is foreach and the 3 dimension arrays, no problem with the looping but i cannot sepcify which array to echo, they must me the whole arrays like echo $subvalue, any better solutions with looping 3 dimension array? i actually feel weird with this looping. Thanks in adv
foreach ($stories as $key => $story){
//echo "<br />";
foreach($story as $subkey => $subvalue){
echo $subvalue."<br />";
foreach($subvalue as $key => $subsubvalue){
echo $subsubvalue."<br />";
}
}
}
Array
(
[270] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 270
[story_name] => Story C
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381418153311.jpg
[1] => story_photos/2012/0322/361332393792911587.jpg
)
[photo_added_date] => Array
(
[0] => 1332381418
[1] => 1332393792
)
)
[269] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 269
[story_name] => Story B
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381406580761.jpg
)
[photo_added_date] => Array
(
[0] => 1332381406
)
)
[268] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 268
[story_name] => Story A
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381393552719.jpg
)
[photo_added_date] => Array
(
[0] => 1332381393
)
)
)
Why not try this :
foreach ($stories as $key => $story){
if(is_array($story)){
foreach($story as $subkey => $subvalue){
if(is_array($subvalue)){
foreach($subvalue as $key => $subsubvalue){
echo $subsubvalue."<br />";
}
} else {
echo $subvalue."<br />";
}
}
} else {
echo $story."<br />";
}
}
Also, I am not sure because your question isn't really clear or specified.
Or
function echoArray( $array )
{
foreach ($array as $key => $cell)
{
if ( true == is_array($cell) )
{
echoArray($cell);
}
else
{
echo "$cell<br />";
}
}
}
It works for N dimensionnal array
An improved version to know the depth and use different css class for depth and to use set the tag in which the value should be added:
Eg: for depth 0 the class will by arrayclass_0, for depth 1 arrayclass_1, etc...
/**
$array : The array
$depth: The depth ( you should always set it to 0)
$cssclassprefix: The css class prefix you want to set
$tag: the default tag to use to render
$arraytagkey: An optionnal key in your array to extract the tag to use
*/
function echoArray( $array, $depth=0, $cssclassprefix='arrayclass_', $tag='div', $arraytagkey = '' )
{
if ( 0 != strcmp($arraytagkey) && isset($array[$arraytagkey]) )
{
$tag=$array[$arraytagkey];
}
foreach ($array as $key => $cell)
{
if ( true == is_array($cell) )
{
echoArray($cell, $depth+1, $cssclassprefix, $tag, $arraytagkey);
}
else
{
$matches = array();
if ( 0 != preg_match("/^(img|iframe|input)$/i",$tag) )
{
if ( 0 != strcasecmp('input',$tag) )
{
echo "<input class='$cssclassprefix$depth' value='$cell' />";
}
else
{
echo "<$tag class='$cssclassprefix$depth' src='$cell' />";
}
}
else if( 0 != preg_match("/^(br|hr)$/i",$tag) )
{
echo "$cell<$tag />";
}
else
{
echo "<$tag class='$cssclassprefix$depth'>$cell</$tag>";
}
}
}
}
This doesn't really answer your question, but note, your third loop is this:
foreach ($subvalue as $key => $subsubvalue){
But you've already used $key in the first loop:
foreach ($stories as $key => $story){
You should change the third to:
foreach ($subvalue as $subsubkey => $subsubvalue){
Are you just wanting to loop through and get access to the photo_urls and other data? If that's the case then here's a simple example of how you can access the data:
foreach($stories as $key => $story)
{
// here you can echo the $store['user_email'] etc
echo 'Email: ' . $story['user_email'] . '<br />';
// loop over the photo urls
foreach($story['photo_url'] as $photo_url)
{
echo 'Photo URL: ' . $photo_url . '<br />';
}
// loop over the photo added dates
foreach($story['photo_added_date'] as $photo_added_date)
{
echo 'Photo Date: ' . $photo_added_date . '<br />';
}
}
If you're wanting to recursively search the array then the other answers are what you want.