how to loop through array and extract another array data php - php

Hi i have a multidimensional array and a parameter that may change frequently .
i made some code to extract data just for 3 level loop through array .
but i can't convert it to function that work in array_map or array_walk .
is it possible ?
sample array data :
array(4) {
[0]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test1"
}
["rel"]=>
array(1) {
["id"]=>
int(1)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test2"
}
["rel"]=>
array(1) {
["id"]=>
int(2)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test2"
}
["rel"]=>
array(1) {
["id"]=>
int(2)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test3"
}
["rel"]=>
array(1) {
["id"]=>
int(3)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test4"
}
["rel"]=>
array(1) {
["id"]=>
int(4)
}
}
}
[1]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test10"
}
["rel"]=>
array(1) {
["id"]=>
int(10)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test11"
}
["rel"]=>
array(1) {
["id"]=>
int(11)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test12"
}
["rel"]=>
array(1) {
["id"]=>
int(12)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test13"
}
["rel"]=>
array(1) {
["id"]=>
int(13)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test14"
}
["rel"]=>
array(1) {
["id"]=>
int(14)
}
}
}
[2]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test20"
}
["rel"]=>
array(1) {
["id"]=>
int(20)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test21"
}
["rel"]=>
array(1) {
["id"]=>
int(21)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test22"
}
["rel"]=>
array(1) {
["id"]=>
int(22)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test23"
}
["rel"]=>
array(1) {
["id"]=>
int(23)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test24"
}
["rel"]=>
array(1) {
["id"]=>
int(24)
}
}
}
}
and here is sample function that can loop 3 times
but i want make it dynamic loop through this array
foreach ( $data as $k => $v ) {
$cnt = 0 ;
foreach ( $data as $m => $n ) {
array_push($parents,$k);
if ( !in_array($m,$parents)) {
foreach ( $data as $x => $y ) {
array_push($parents,$m);
if ( !in_array($x,$parents)) {
$hold[$k][$cnt] = array(
$k , $m , $x
);
$cnt++;
}
}
$parents = deleteFromArray($parents , $m);
}
}
$parents = deleteFromArray($parents , $k);
}

Related

How to output all flights in HTML with this nested array?

Just want to output the array I get using: https://github.com/dahabtours/amadeus-php-sdk
$date_checkin = '2019-07-16';
$date_checkout = '2019-07-18';
require('AmadeusClient.php');
$amadeus_api = new AmadeusDahabtours\SelfServiceApiClient('CLIENT_ID','CLIENT_SECRET');
# Flight Low-fare Search
$flight_offers = $amadeus_api->lowFares([
'origin' => 'AMS',
'destination' => 'BCN',
'departureDate' => $date_checkin,
'returnDate' => $date_checkout,
'nonStop' => true,
'currency' => 'EUR',
'max' => 2,
]);
I want the output to be something like this:
<div class="flights">
<div class="flight-1">VALUES</div>
<div class="flight-2">VALUES</div>
<div class="flight-3">VALUES</div>
</div>
Array:
array(5) {
["success"]=>
bool(true)
["msgs"]=>
array(0) {
}
["http_code"]=>
int(200)
["response_text"]=>
string(2597) "{"data":[{"type":"flight-offer","id":"1562680375384--1832628087","offerItems":[{"services":[{"segments":[{"flightSegment":{"departure":{"iataCode":"AMS","terminal":"3","at":"2019-07-16T07:00:00+02:00"},"arrival":{"iataCode":"BCN","terminal":"1","at":"2019-07-16T09:20:00+02:00"},"carrierCode":"IB","number":"5224","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"5224"},"duration":"0DT2H20M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"V","availability":4,"fareBasis":"VRTNVY"}}]},{"segments":[{"flightSegment":{"departure":{"iataCode":"BCN","terminal":"1","at":"2019-07-18T12:05:00+02:00"},"arrival":{"iataCode":"AMS","at":"2019-07-18T14:30:00+02:00"},"carrierCode":"IB","number":"5685","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"5685"},"duration":"0DT2H25M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"A","availability":4,"fareBasis":"ARTNVY"}}]}],"price":{"total":"209.09","totalTaxes":"74.09"},"pricePerAdult":{"total":"209.09","totalTaxes":"74.09"}}]},{"type":"flight-offer","id":"1562680375384--510820791","offerItems":[{"services":[{"segments":[{"flightSegment":{"departure":{"iataCode":"AMS","at":"2019-07-16T07:00:00+02:00"},"arrival":{"iataCode":"BCN","terminal":"1","at":"2019-07-16T09:20:00+02:00"},"carrierCode":"VY","number":"8318","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"8318"},"duration":"0DT2H20M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"X","availability":6,"fareBasis":"XRTVY"}}]},{"segments":[{"flightSegment":{"departure":{"iataCode":"BCN","terminal":"1","at":"2019-07-18T12:05:00+02:00"},"arrival":{"iataCode":"AMS","at":"2019-07-18T14:30:00+02:00"},"carrierCode":"VY","number":"8302","aircraft":{"code":"321"},"operating":{"carrierCode":"VY","number":"8302"},"duration":"0DT2H25M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"P","availability":8,"fareBasis":"PRTVY"}}]}],"price":{"total":"209.09","totalTaxes":"74.09"},"pricePerAdult":{"total":"209.09","totalTaxes":"74.09"}}]}],"dictionaries":{"carriers":{"VY":"VUELING AIRLINES","IB":"IBERIA"},"currencies":{"EUR":"EURO"},"aircraft":{"320":"AIRBUS INDUSTRIE A320-100/200","321":"AIRBUS INDUSTRIE A321"},"locations":{"AMS":{"subType":"AIRPORT","detailedName":"SCHIPHOL AIRPORT"},"BCN":{"subType":"AIRPORT","detailedName":"AIRPORT"}}},"meta":{"links":{"self":"https://test.api.amadeus.com/v1/shopping/flight-offers?origin=AMS&destination=BCN&departureDate=2019-07-16&returnDate=2019-07-18&adults=1&nonStop=true¤cy=EUR&max=2"},"currency":"EUR","defaults":{"adults":1}}}"
["response"]=>
array(3) {
["data"]=>
array(2) {
[0]=>
array(3) {
["type"]=>
string(12) "flight-offer"
["id"]=>
string(25) "1562680375384--1832628087"
["offerItems"]=>
array(1) {
[0]=>
array(3) {
["services"]=>
array(2) {
[0]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "AMS"
["terminal"]=>
string(1) "3"
["at"]=>
string(25) "2019-07-16T07:00:00+02:00"
}
["arrival"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-16T09:20:00+02:00"
}
["carrierCode"]=>
string(2) "IB"
["number"]=>
string(4) "5224"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "5224"
}
["duration"]=>
string(8) "0DT2H20M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "V"
["availability"]=>
int(4)
["fareBasis"]=>
string(6) "VRTNVY"
}
}
}
}
[1]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-18T12:05:00+02:00"
}
["arrival"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-18T14:30:00+02:00"
}
["carrierCode"]=>
string(2) "IB"
["number"]=>
string(4) "5685"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "5685"
}
["duration"]=>
string(8) "0DT2H25M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "A"
["availability"]=>
int(4)
["fareBasis"]=>
string(6) "ARTNVY"
}
}
}
}
}
["price"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
["pricePerAdult"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
}
}
}
[1]=>
array(3) {
["type"]=>
string(12) "flight-offer"
["id"]=>
string(24) "1562680375384--510820791"
["offerItems"]=>
array(1) {
[0]=>
array(3) {
["services"]=>
array(2) {
[0]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-16T07:00:00+02:00"
}
["arrival"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-16T09:20:00+02:00"
}
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8318"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8318"
}
["duration"]=>
string(8) "0DT2H20M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "X"
["availability"]=>
int(6)
["fareBasis"]=>
string(5) "XRTVY"
}
}
}
}
[1]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-18T12:05:00+02:00"
}
["arrival"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-18T14:30:00+02:00"
}
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8302"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "321"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8302"
}
["duration"]=>
string(8) "0DT2H25M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "P"
["availability"]=>
int(8)
["fareBasis"]=>
string(5) "PRTVY"
}
}
}
}
}
["price"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
["pricePerAdult"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
}
}
}
}
["dictionaries"]=>
array(4) {
["carriers"]=>
array(2) {
["VY"]=>
string(16) "VUELING AIRLINES"
["IB"]=>
string(6) "IBERIA"
}
["currencies"]=>
array(1) {
["EUR"]=>
string(4) "EURO"
}
["aircraft"]=>
array(2) {
[320]=>
string(29) "AIRBUS INDUSTRIE A320-100/200"
[321]=>
string(21) "AIRBUS INDUSTRIE A321"
}
["locations"]=>
array(2) {
["AMS"]=>
array(2) {
["subType"]=>
string(7) "AIRPORT"
["detailedName"]=>
string(16) "SCHIPHOL AIRPORT"
}
["BCN"]=>
array(2) {
["subType"]=>
string(7) "AIRPORT"
["detailedName"]=>
string(7) "AIRPORT"
}
}
}
["meta"]=>
array(3) {
["links"]=>
array(1) {
["self"]=>
string(169) "https://test.api.amadeus.com/v1/shopping/flight-offers?origin=AMS&destination=BCN&departureDate=2019-07-16&returnDate=2019-07-18&adults=1&nonStop=true¤cy=EUR&max=2"
}
["currency"]=>
string(3) "EUR"
["defaults"]=>
array(1) {
["adults"]=>
int(1)
}
}
}
}
Now i have this:
foreach ( $flight_offers as $flights ) {
if (is_array( $flights ) ){
foreach ( $flights as $value ) {
echo $value;
}
} else {
echo $flights;
}
}

Fatal error: Cannot use string offset as an array - What the right way to compare array value

I call to array and try to compare the values, is there something wrong in my syntax?
foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
$field = "100";
if ($item["#attributes"]["tag"] == $field) {
}
}
This is my array:
array(1) { ["inf"]=> array(9) { ["hid"]=> string(13) "4754745675467" ["created_by"]=> string(6) "import" ["created_date"]=> string(11) "2017-01-01Z" ["last_modified_by"]=> string(13) "Update Record" ["last_modified_date"]=> string(11) "2018-01-2Z" ["originating_system"]=> string(3) "rrr" ["orid"]=> string(15) "1234565432167854" ["supp"]=> string(5) "false" ["rec"]=> array(3) { ["lead"]=> string(3) "500" ["field"]=> array(2) { [0]=> array(2) { ["#value"]=> string(5) "22333" ["#attributes"]=> array(1) { ["tag"]=> string(3) "001" } } [1]=> array(2) { ["#value"]=> string(3) "110" ["#attributes"]=> array(1) { ["tag"]=> string(3) "001" } } } ["dfield"]=> array(2) { [0]=> array(2) { ["subfield"]=> array(2) { ["#value"]=> string(2) "92" ["#attributes"]=> array(1) { ["cod"]=> string(1) "a" } } ["#attributes"]=> array(3) { ["ind1"]=> string(1) " " ["ind2"]=> string(1) " " ["tag"]=> string(3) "101" } } [1]=> array(2) { ["subfield"]=> array(3) { [0]=> array(2) { ["#value"]=> string(4) "ntft" ["#attributes"]=> array(1) { ["cod"]=> string(1) "b" } } [1]=> array(2) { ["#value"]=> string(5) "nthgfr" ["#attributes"]=> array(1) { ["cod"]=> string(1) "c" } } [2]=> array(2) { ["#value"]=> string(5) "test2" ["#attributes"]=> array(1) { ["cod"]=> string(1) "z" } } } ["#attributes"]=> array(3) { ["ind1"]=> string(1) "1" ["ind2"]=> string(1) " " ["tag"]=> string(3) "100" } } } } } }
I compare `tag = 100` to a variable with value 100: `if ($item["#attributes"]["tag"] == $field)`
This array I received after changes that used from last discussion from this post:
array(1) { ["inf"]=> array(9) { ["hid"]=> string(13) "4754745675467" ["created_by"]=> string(6) "import" ["created_date"]=> string(11) "2017-01-01Z" ["last_modified_by"]=> string(13) "Update Record" ["last_modified_date"]=> string(11) "2018-01-2Z" ["originating_system"]=> string(3) "rrr" ["orid"]=> string(15) "1234565432167854" ["supp"]=> string(5) "false" ["rec"]=> array(3) { ["lead"]=> string(3) "500" ["field"]=> array(2) { [0]=> array(2) { ["#value"]=> string(5) "22333" ["#attributes"]=> array(1) { ["tag"]=> string(3) "001" } } [1]=> array(2) { ["#value"]=> string(3) "110" ["#attributes"]=> array(1) { ["tag"]=> string(3) "001" } } } ["dfield"]=> array(3) { [0]=> array(2) { ["subfield"]=> array(2) { ["#value"]=> string(2) "92" ["#attributes"]=> array(1) { ["cod"]=> string(1) "a" } } ["#attributes"]=> array(3) { ["ind1"]=> string(1) " " ["ind2"]=> string(1) " " ["tag"]=> string(3) "101" } } [1]=> array(2) { ["subfield"]=> array(3) { [0]=> array(2) { ["#value"]=> string(4) "ntft" ["#attributes"]=> array(1) { ["code"]=> string(1) "b" } } [1]=> array(2) { ["#value"]=> string(5) "nthgfr" ["#attributes"]=> array(1) { ["code"]=> string(1) "c" } } [2]=> array(2) { ["#value"]=> string(4) "test" ["#attributes"]=> array(1) { ["cod"]=> string(1) "z" } } } ["#attributes"]=> array(3) { ["ind1"]=> string(1) "1" ["ind2"]=> string(1) " " ["tag"]=> string(3) "100" } } ["subfield"]=> array(1) { [2]=> array(1) { ["#value"]=> string(12) "26A 1 2 test" } } } } } }
You're using ["#attributes"] but $item is not a "standard" array. It seems to be a SimpleXMLElement object.
Try to use the following code :
foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
$field = "100";
if ((string)$item["tag"] == $field) {
var_dump("Equals") ;
}
}
edit
$field = "100";
foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
var_dump($key_item) ;
if (is_array($item)) {
foreach ($item as $key_element => $element) {
var_dump($key_element) ;
if (!isset($element["#attributes"])) { echo("no attribute"); continue ; }
if (!isset($element["#attributes"]['tag'])) { echo("no tag"); continue; }
if ($element["#attributes"]["tag"] == $field) {
var_dump("match") ;
}
}
}
else{
echo "item is not an array" ;
}
}

Loop through PHP multidimensional array

I am trying to loop through a multidimensional array but in the foreach loop it just outputs error
index 'name' not found. index 'calories' not founder
foreach($responsex['foods'] as $fx5)
{
echo($fx5['name']);
echo($fx5['calories']);
}
Response: i.e. $responsex
array ( 'encodedId' => '4H8xxx', 'displayName' => 'sam', )array(3) {
["foods"]=> array(3) { [0]=> array(5) { ["isFavorite"]=> bool(false)
["logDate"]=> string(10) "2016-04-15" ["logId"]=> int(7139364449)
["loggedFood"]=> array(10) { ["accessLevel"]=> string(6) "PUBLIC"
["amount"]=> int(2) ["brand"]=> string(0) "" ["calories"]=> int(574)
["foodId"]=> int(536497687) ["locale"]=> string(5) "en_AU"
["mealTypeId"]=> int(7) ["name"]=> string(14) "Potato Pudding"
["unit"]=> array(3) { ["id"]=> int(91) ["name"]=> string(3) "cup"
["plural"]=> string(4) "cups" } ["units"]=> array(8) { [0]=> int(6754)
[1]=> int(91) [2]=> int(256) [3]=> int(279) [4]=> int(226) [5]=>
int(180) [6]=> int(147) [7]=> int(389) } } ["nutritionalValues"]=>
array(6) { ["calories"]=> int(574) ["carbs"]=> float(49.16) ["fat"]=>
float(34.98) ["fiber"]=> float(3.6) ["protein"]=> float(16.1)
["sodium"]=> int(1524) } } [1]=> array(5) { ["isFavorite"]=>
bool(false) ["logDate"]=> string(10) "2016-04-15" ["logId"]=>
int(7138517833) ["loggedFood"]=> array(10) { ["accessLevel"]=>
string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=> string(0) ""
["calories"]=> int(359) ["foodId"]=> int(535239347) ["locale"]=>
string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=> string(54) "Fish,
Noodles and Vegetables in Cheese Sauce (Mixture)" ["unit"]=> array(3)
{ ["id"]=> int(91) ["name"]=> string(3) "cup" ["plural"]=> string(4)
"cups" } ["units"]=> array(8) { [0]=> int(6837) [1]=> int(91) [2]=>
int(256) [3]=> int(279) [4]=> int(226) [5]=> int(180) [6]=> int(147)
[7]=> int(389) } } ["nutritionalValues"]=> array(6) { ["calories"]=>
int(359) ["carbs"]=> float(28.01) ["fat"]=> float(14.05) ["fiber"]=>
float(2.9) ["protein"]=> float(29.08) ["sodium"]=> int(534) } } [2]=>
array(5) { ["isFavorite"]=> bool(false) ["logDate"]=> string(10)
"2016-04-15" ["logId"]=> int(7138326866) ["loggedFood"]=> array(10) {
["accessLevel"]=> string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=>
string(0) "" ["calories"]=> int(157) ["foodId"]=> int(536493638)
["locale"]=> string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=>
string(11) "Cashew Nuts" ["unit"]=> array(3) { ["id"]=> int(226)
["name"]=> string(2) "oz" ["plural"]=> string(2) "oz" } ["units"]=>
array(4) { [0]=> int(226) [1]=> int(180) [2]=> int(147) [3]=> int(389)
} } ["nutritionalValues"]=> array(6) { ["calories"]=> int(157)
["carbs"]=> float(8.56) ["fat"]=> float(12.43) ["fiber"]=> float(0.9)
["protein"]=> float(5.17) ["sodium"]=> int(3) } } } ["goals"]=>
array(2) { ["calories"]=> int(1161) ["estimatedCaloriesOut"]=>
int(1411) } ["summary"]=> array(7) { ["calories"]=> int(1090)
["carbs"]=> float(85.73) ["fat"]=> float(61.46) ["fiber"]=> float(7.4)
["protein"]=> float(50.35) ["sodium"]=> int(2061) ["water"]=> int(0) }
}
you can recursively iterate through the arrays and print them as follows as key value pairs.
<?php
//initially call the function
print_array($responsex);
function print_array($array){
foreach($array as $key=>$value){
//recursively print the array
if(is_array($value)){
echo("Array : ".$key."\n");
print_array($value);
}
else{
echo($key." => ".$value);
}
}
}
?>
You can define additional tasks other than printing them with the above code.
Edit:
if you are sure that the array is two dimensional, no need to go recursively.
<?php
//initially call the function
print_array($responsex);
//if you are sure that the array is two dimensional, no need to go recursively.
function print_array($array){
foreach($array as $key=>$value){
if(is_array($value)){
if($key==="foods"){
var_dump($array[$key]);
}
}
else{
echo($key." => ".$value);
}
}
}
Use this way..
<?php
$keys = array_keys($data);// put your array name as a place of $data
$iterations = count($array[$keys[0]]);
for($i = 0; $i < $iterations; $i++) {
$data = array();
foreach($array as $key => $value) {
$data[$key] = $value[$i];
}
print_r($data);
}
?>

PHP: Count values in Arrays

I have this Array:
array(66) {
[0]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105a"
}
[1]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105b"
}
[2]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105c"
}
[3]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105d"
}
[4]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117b"
}
[5]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117c"
}
[6]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "123a"
}
[7]=> array(2) {
["location"]=> string(10) "Whateverelse" ["bsid"]=> string(4) "123b"
}
}
How can I count how many Breakwater's I have and how many Whateverelse's and get something like this:
array(2) {
[0]=> array(2) {
["Breakwater"]=> string(2) "20"
} [1]=> array(2) {
["Whateverelse"]=> string(1) "1"
}
}
Just loop around the original array, and each time a location is hit, increment a counter in the locations array where the index is the location.
$loc = array();
foreach($arr as $value) {
$location = $value['location'];
if(isset($loc[$location])) {
$loc[$location]++;
} else {
$loc[$location] = 1;
}
}
print_r($loc);
Will output
array
(
["Breakwater"] => 7,
["Whateverelse"] => 1
}
I got this finally :
$output = array("Breakwater" => 0, "Whateverelse" => 0);
foreach ($array as $val) {
$output["Breakwater"] += ($val["location"] == "Breakwater") ? 1 : 0;
$output["Whateverelse"] += ($val["location"] == "Whateverelse") ? 1 : 0;
}
var_dump($output);

Get value from Object array

Update, solved
I try to retrieve the value of an object (stdClass)
I want to get the internship his name the title.
(["title"]=> string(53) "Mss ( Privaatpraktijk kinesitherapie Walravens Marc )").
My code:
foreach($gStartPositionStudents as $value) {
foreach($gStartPositionInternships as $value2) {
// each loop will add a row here.
$row = array();
// build the row
$row[] = array('data' => $value->field_ugentid_student[0]['value']);
$row[] = array('data' => $value2->title[0]['value']);
}
// add the row to the "big row data (contains all rows)
$rows[] = array('data' => $row);
}
$output = theme('table', $header, $rows);
return $output;
Object array:
enter code here
array(148) {
[0]=>
object(stdClass)#466 (45) {
["nid"]=>
string(3) "199"
["type"]=>
string(11) "stageplaats"
["language"]=>
string(2) "nl"
["uid"]=>
string(2) "33"
["status"]=>
string(1) "1"
["created"]=>
string(10) "1291279334"
["changed"]=>
string(10) "1301323146"
["comment"]=>
string(1) "0"
["promote"]=>
string(1) "0"
["moderate"]=>
string(1) "0"
["sticky"]=>
string(1) "0"
["tnid"]=>
string(1) "0"
["translate"]=>
string(1) "0"
["vid"]=>
string(3) "206"
["revision_uid"]=>
string(2) "16"
["title"]=>
string(18) "Pcv (campus Aalst)"
["body"]=>
string(0) ""
["teaser"]=>
string(0) ""
["log"]=>
string(0) ""
["revision_timestamp"]=>
string(10) "1301323146"
["format"]=>
string(1) "0"
["nd_switch_bm"]=>
string(0) ""
["name"]=>
string(12) "stage.revaki"
["picture"]=>
string(0) ""
["data"]=>
string(72) "a:1:{s:13:"form_build_id";s:37:"form-f89b99e4a5249b192ff472579b826b00";}"
["field_stagedomein_audiologo"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_geneeskunde"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_lo"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_revaki"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(3) "152"
}
}
["field_stagedomein_verpleeg"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_status"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(14) "niet opgegeven"
}
}
["field_status_datum"]=>
array(1) {
[0]=>
array(4) {
["value"]=>
NULL
["timezone"]=>
string(15) "Europe/Brussels"
["timezone_db"]=>
string(15) "Europe/Brussels"
["date_type"]=>
string(4) "date"
}
}
["field_locatieview"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
string(3) "179"
}
}
["field_status_extra"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_status_stagedomein"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(6) "Actief"
}
}
["field_maxcapstud_stagedomein"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(2) "18"
}
}
["field_status_bevestigd_door"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
NULL
}
}
["field_aanpers_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
NULL
}
}
["field_extrainfo_stagedomein"]=>
array(1) {
[0]=>
array(2) {
["value"]=>
string(37) "Capaciteit: 3 studenten alle periodes"
["format"]=>
NULL
}
}
["field_percapaciteit_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_aanplaats_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_mentoren"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
string(3) "178"
}
}
["field_persoon_stagedomein"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
NULL
}
}
["has_body"]=>
string(1) "0"
["taxonomy"]=>
array(0) {
}
}
[1]=>
object(stdClass)#469 (45) {
["nid"]=>
string(3) "211"
["type"]=>
string(11) "stageplaats"
["language"]=>
string(2) "nl"
["uid"]=>
string(2) "33"
["status"]=>
string(1) "1"
["created"]=>
string(10) "1291281467"
["changed"]=>
string(10) "1301323440"
["comment"]=>
string(1) "0"
["promote"]=>
string(1) "0"
["moderate"]=>
string(1) "0"
["sticky"]=>
string(1) "0"
["tnid"]=>
string(1) "0"
["translate"]=>
string(1) "0"
["vid"]=>
string(3) "218"
["revision_uid"]=>
string(2) "16"
["title"]=>
string(53) "Mss ( Privaatpraktijk kinesitherapie Walravens Marc )"
["body"]=>
string(0) ""
["teaser"]=>
string(0) ""
["log"]=>
string(0) ""
["revision_timestamp"]=>
string(10) "1301323440"
["format"]=>
string(1) "0"
["nd_switch_bm"]=>
string(0) ""
["name"]=>
string(12) "stage.revaki"
["picture"]=>
string(0) ""
["data"]=>
string(72) "a:1:{s:13:"form_build_id";s:37:"form-f89b99e4a5249b192ff472579b826b00";}"
["field_stagedomein_audiologo"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_geneeskunde"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_lo"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_revaki"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(3) "128"
}
}
["field_stagedomein_verpleeg"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_status"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(14) "niet opgegeven"
}
}
["field_status_datum"]=>
array(1) {
[0]=>
array(4) {
["value"]=>
NULL
["timezone"]=>
string(15) "Europe/Brussels"
["timezone_db"]=>
string(15) "Europe/Brussels"
["date_type"]=>
string(4) "date"
}
}
["field_locatieview"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
string(3) "210"
}
}
["field_status_extra"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_status_stagedomein"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(6) "Actief"
}
}
["field_maxcapstud_stagedomein"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(1) "4"
}
}
["field_status_bevestigd_door"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
NULL
}
}
["field_aanpers_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
NULL
}
}
["field_extrainfo_stagedomein"]=>
array(1) {
[0]=>
array(2) {
["value"]=>
string(40) "Capaciteit: 1 student in periode 3 tem 6"
["format"]=>
NULL
}
}
["field_percapaciteit_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_aanplaats_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_mentoren"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
string(3) "208"
}
}
["field_persoon_stagedomein"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
NULL
}
}
["has_body"]=>
string(1) "0"
["taxonomy"]=>
array(0) {
}
}
[2]=>
object(stdClass)#472 (45) {
["nid"]=>
string(3) "217"
["type"]=>
string(11) "stageplaats"
["language"]=>
string(2) "nl"
["uid"]=>
string(2) "33"
["status"]=>
string(1) "1"
["created"]=>
string(10) "1291281906"
["changed"]=>
string(10) "1301323440"
["comment"]=>
string(1) "0"
["promote"]=>
string(1) "0"
["moderate"]=>
string(1) "0"
["sticky"]=>
string(1) "0"
["tnid"]=>
string(1) "0"
["translate"]=>
string(1) "0"
["vid"]=>
string(3) "224"
["revision_uid"]=>
string(2) "16"
["title"]=>
string(54) "Mss ( Privaatpraktijk kinesitherapie Peeters Stefaan )"
["body"]=>
string(0) ""
["teaser"]=>
string(0) ""
["log"]=>
string(0) ""
["revision_timestamp"]=>
string(10) "1301323440"
["format"]=>
string(1) "0"
["nd_switch_bm"]=>
string(0) ""
["name"]=>
string(12) "stage.revaki"
["picture"]=>
string(0) ""
["data"]=>
string(72) "a:1:{s:13:"form_build_id";s:37:"form-f89b99e4a5249b192ff472579b826b00";}"
["field_stagedomein_audiologo"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_geneeskunde"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_lo"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_stagedomein_revaki"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(3) "128"
}
}
["field_stagedomein_verpleeg"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_status"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(14) "niet opgegeven"
}
}
["field_status_datum"]=>
array(1) {
[0]=>
array(4) {
["value"]=>
NULL
["timezone"]=>
string(15) "Europe/Brussels"
["timezone_db"]=>
string(15) "Europe/Brussels"
["date_type"]=>
string(4) "date"
}
}
["field_locatieview"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
string(3) "213"
}
}
["field_status_extra"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_status_stagedomein"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(6) "Actief"
}
}
["field_maxcapstud_stagedomein"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
string(1) "6"
}
}
["field_status_bevestigd_door"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
NULL
}
}
["field_aanpers_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
NULL
}
}
["field_extrainfo_stagedomein"]=>
array(1) {
[0]=>
array(2) {
["value"]=>
string(38) "Capaciteit: 1 student in alle periodes"
["format"]=>
NULL
}
}
["field_percapaciteit_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_aanplaats_stageplaats"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
NULL
}
}
["field_mentoren"]=>
array(1) {
[0]=>
array(1) {
["nid"]=>
string(3) "214"
}
}
["field_persoon_stagedomein"]=>
array(2) {
[0]=>
array(1) {
["nid"]=>
string(3) "216"
}
[1]=>
array(1) {
["nid"]=>
string(3) "215"
}
}
["has_body"]=>
string(1) "0"
["taxonomy"]=>
array(0) {
}
}
Well, as far as I can tell, you've actually assigned an array to the numerous variables in the object. As a result, you will need to do something like this in your foreach loop:
global $gStartPositionStudents;
global $gStartPositionInternships;
$rows = array();
foreach($gStartPositionStudents as $value) {
// each loop will add a row here.
$row = array();
// build the row
$row[] = array('data' => $value->field_naam_student[0]['value']);
$row[] = array('data' => $gStartPositionInternships->field_stagedomein_revaki);
// add the row to the "big row data (contains all rows)
$rows[] = array('data' => $row);
}
$output = theme('table', $header, $rows);
return $output;

Categories