PHP Webservice not being consumed - php

I'm using nusoap and I'm having dificulties in consuming webservices ... What I'm trying to return to who consumes one of the webservices is one array ...
In the first case I'm trying to return this type of array:
Array ( [0] => EX123EX [1] => Test [2] => 2013/04/27 [3] => 12:06 [4] => This is a test [5] => [+]Info [-]Info )
And for this I have:
$server->wsdl->addComplexType(
'details',
'complexType',
'struct',
'all',
'',
array(
'id' => array('name' => 'id', 'type' => 'xsd:string'),
'product' => array('name' => 'product', 'type' => 'xsd:string'),
'date' => array('name' => 'date', 'type' => 'xsd:string'),
'hour' => array('name' => 'hour', 'type' => 'xsd:string'),
'status' => array('name' => 'status', 'type' => 'xsd:string'),
'info' => array('name' => 'info', 'type' => 'xsd:string'),
));
$server->register(
'getdetails',
array('url' => 'xsd:string'),
array('return' => 'tns:details'),
$namespace,
false,
'rpc',
'literal',
'details');
and the function:
function getdetalhes($url)
{
$details = getHeader($url);
return $details;
}
The problem is that the webservice is not consumed... When I make the request I have no answer ... With this error I'm also unable to keep on for the next webservice that would return an array with this structure:
Array ( [0] => Array ( ) [1] => Array ( [0] => 2012/12/13 [1] => 12:06 [2] => Test [3] => - [4] => Test Test [5] => Test ) [2] => Array ( [0] => 2012/12/13 [1] => 09:23 [2] => Test Test [3] => - [4] => Test [5] => - ) [3] => Array ( [0] => 2012/12/12 [1] => 17:43 [2] => Test [3] => - [4] => Test [5] => - ) [4] => Array ( [0] => 2012/12/12 [1] => 11:25 [2] => Test [3] => Test [4] =>Test [5] => - )
I'm confident that the error is on the complexType declaration but I can't figure out what is the problem, can someone help me please?

Please, make sure that you're accessing the associative keywords of the array instead of indexes :)

Related

how to do a foreach loop values in a multidimensional array PHP

I'm working with Spotify API and I want to do a foreach loop for only the values in ['tracks'][0]["album"]["images"][2]['url']
How can I go about this?
I tried to do a foreach loop to retrieve value of each key associated inside the multidimensional associative array iterating through each element but can still get it to work.
Array
(
[tracks] => Array
(
[0] => Array
(
[album] => Array
(
[album_type] => album
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/album/6fG2eFCgUmytQWL6umtsCh
)
[href] => https://api.spotify.com/v1/albums/6fG2eFCgUmytQWL6umtsCh
[id] => 6fG2eFCgUmytQWL6umtsCh
[images] => Array
(
[0] => Array
(
[height] => 640
[url] => https://i.scdn.co/image/ab67616d0000b27387d20b9a27d5e14d74b5cb77
[width] => 640
)
[1] => Array
(
[height] => 300
[url] => https://i.scdn.co/image/ab67616d00001e0287d20b9a27d5e14d74b5cb77
[width] => 300
)
[2] => Array
(
[height] => 64
[url] => https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77
[width] => 64
)
)
[name] => Carpe Diem
[release_date] => 2020-10-07
[release_date_precision] => day
[total_tracks] => 12
[type] => album
[uri] => spotify:album:6fG2eFCgUmytQWL6umtsCh
)
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
[1] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/5yOvAmpIR7hVxiS6Ls5DPO
)
[href] => https://api.spotify.com/v1/artists/5yOvAmpIR7hVxiS6Ls5DPO
[id] => 5yOvAmpIR7hVxiS6Ls5DPO
[name] => Omah Lay
[type] => artist
[uri] => spotify:artist:5yOvAmpIR7hVxiS6Ls5DPO
)
)
[disc_number] => 1
[duration_ms] => 171764
[explicit] => 1
[external_ids] => Array
(
[isrc] => USUYG1330802
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/track/5DS9LiyEdw2zY8bM6kjjgM
)
[href] => https://api.spotify.com/v1/tracks/5DS9LiyEdw2zY8bM6kjjgM
[id] => 5DS9LiyEdw2zY8bM6kjjgM
[is_local] =>
[is_playable] => 1
[name] => Infinity (feat. Omah Lay)
[popularity] => 73
[preview_url] => https://p.scdn.co/mp3-preview/5159fa17676fc96e32db13b7680f7497d5c2f74d?cid=756f211306ad4c9a934d07c5722790b5
[track_number] => 3
[type] => track
[uri] => spotify:track:5DS9LiyEdw2zY8bM6kjjgM
)
[1] => Array
(
[album] => Array
(
[album_type] => album
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/album/6fG2eFCgUmytQWL6umtsCh
)
[href] => https://api.spotify.com/v1/albums/6fG2eFCgUmytQWL6umtsCh
[id] => 6fG2eFCgUmytQWL6umtsCh
[images] => Array
(
[0] => Array
(
[height] => 640
[url] => https://i.scdn.co/image/ab67616d0000b27387d20b9a27d5e14d74b5cb77
[width] => 640
)
[1] => Array
(
[height] => 300
[url] => https://i.scdn.co/image/ab67616d00001e0287d20b9a27d5e14d74b5cb77
[width] => 300
)
[2] => Array
(
[height] => 64
[url] => https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77
[width] => 64
)
)
[name] => Carpe Diem
[release_date] => 2020-10-07
[release_date_precision] => day
[total_tracks] => 12
[type] => album
[uri] => spotify:album:6fG2eFCgUmytQWL6umtsCh
)
[artists] => Array
(
[0] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr
)
[href] => https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr
[id] => 4ovtyvs7j1jSmwhkBGHqSr
[name] => Olamide
[type] => artist
[uri] => spotify:artist:4ovtyvs7j1jSmwhkBGHqSr
)
[1] => Array
(
[external_urls] => Array
(
[spotify] => https://open.spotify.com/artist/68R39izwNAztATrXMOqkJS
)
[href] => https://api.spotify.com/v1/artists/68R39izwNAztATrXMOqkJS
[id] => 68R39izwNAztATrXMOqkJS
[name] => Bad Boy Timz
[type] => artist
[uri] => spotify:artist:68R39izwNAztATrXMOqkJS
)
)
[disc_number] => 1
[duration_ms] => 194000
[explicit] => 1
[external_ids] => Array
(
[isrc] => USUYG1330809
)
[external_urls] => Array
(
[spotify] => https://open.spotify.com/track/558ULLj8yY2vT8XGtgY0q9
)
[href] => https://api.spotify.com/v1/tracks/558ULLj8yY2vT8XGtgY0q9
[id] => 558ULLj8yY2vT8XGtgY0q9
[is_local] =>
[is_playable] => 1
[name] => Loading (feat. Bad Boy Timz)
[popularity] => 64
[preview_url] => https://p.scdn.co/mp3-preview/7848a9f9ea879e6b1c6618508354e2559678aa8c?cid=756f211306ad4c9a934d07c5722790b5
[track_number] => 10
[type] => track
[uri] => spotify:track:558ULLj8yY2vT8XGtgY0q9
)
I would approach it like this:
$api = array (
'tracks' =>
array (
0 =>
array (
'album' =>
array (
'album_type' => 'album',
'artists' =>
array (
0 =>
array (
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr',
),
'href' => 'https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr',
'id' => '4ovtyvs7j1jSmwhkBGHqSr',
'name' => 'Olamide',
'type' => 'artist',
'uri' => 'spotify:artist:4ovtyvs7j1jSmwhkBGHqSr',
),
),
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/album/6fG2eFCgUmytQWL6umtsCh',
),
'href' => 'https://api.spotify.com/v1/albums/6fG2eFCgUmytQWL6umtsCh',
'id' => '6fG2eFCgUmytQWL6umtsCh',
'images' =>
array (
0 =>
array (
'height' => '640',
'url' => 'https://i.scdn.co/image/ab67616d0000b27387d20b9a27d5e14d74b5cb77',
'width' => '640',
),
1 =>
array (
'height' => '300',
'url' => 'https://i.scdn.co/image/ab67616d00001e0287d20b9a27d5e14d74b5cb77',
'width' => '300',
),
2 =>
array (
'height' => '64',
'url' => 'https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77',
'width' => '64',
),
),
'name' => 'Carpe Diem',
'release_date' => '2020-10-07',
'release_date_precision' => 'day',
'total_tracks' => '12',
'type' => 'album',
'uri' => 'spotify:album:6fG2eFCgUmytQWL6umtsCh',
),
'artists' =>
array (
0 =>
array (
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr',
),
'href' => 'https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr',
'id' => '4ovtyvs7j1jSmwhkBGHqSr',
'name' => 'Olamide',
'type' => 'artist',
'uri' => 'spotify:artist:4ovtyvs7j1jSmwhkBGHqSr',
),
1 =>
array (
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/artist/5yOvAmpIR7hVxiS6Ls5DPO',
),
'href' => 'https://api.spotify.com/v1/artists/5yOvAmpIR7hVxiS6Ls5DPO',
'id' => '5yOvAmpIR7hVxiS6Ls5DPO',
'name' => 'Omah Lay',
'type' => 'artist',
'uri' => 'spotify:artist:5yOvAmpIR7hVxiS6Ls5DPO',
),
),
'disc_number' => '1',
'duration_ms' => '171764',
'explicit' => '1',
'external_ids' =>
array (
'isrc' => 'USUYG1330802',
),
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/track/5DS9LiyEdw2zY8bM6kjjgM',
),
'href' => 'https://api.spotify.com/v1/tracks/5DS9LiyEdw2zY8bM6kjjgM',
'id' => '5DS9LiyEdw2zY8bM6kjjgM
[is_local] => ',
'is_playable' => '1',
'name' => 'Infinity (feat. Omah Lay)',
'popularity' => '73',
'preview_url' => 'https://p.scdn.co/mp3-preview/5159fa17676fc96e32db13b7680f7497d5c2f74d?cid=756f211306ad4c9a934d07c5722790b5',
'track_number' => '3',
'type' => 'track',
'uri' => 'spotify:track:5DS9LiyEdw2zY8bM6kjjgM',
),
1 =>
array (
'album' =>
array (
'album_type' => 'album',
'artists' =>
array (
0 =>
array (
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr',
),
'href' => 'https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr',
'id' => '4ovtyvs7j1jSmwhkBGHqSr',
'name' => 'Olamide',
'type' => 'artist',
'uri' => 'spotify:artist:4ovtyvs7j1jSmwhkBGHqSr',
),
),
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/album/6fG2eFCgUmytQWL6umtsCh',
),
'href' => 'https://api.spotify.com/v1/albums/6fG2eFCgUmytQWL6umtsCh',
'id' => '6fG2eFCgUmytQWL6umtsCh',
'images' =>
array (
0 =>
array (
'height' => '640',
'url' => 'https://i.scdn.co/image/ab67616d0000b27387d20b9a27d5e14d74b5cb77',
'width' => '640',
),
1 =>
array (
'height' => '300',
'url' => 'https://i.scdn.co/image/ab67616d00001e0287d20b9a27d5e14d74b5cb77',
'width' => '300',
),
2 =>
array (
'height' => '64',
'url' => 'https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77',
'width' => '64',
),
),
'name' => 'Carpe Diem',
'release_date' => '2020-10-07',
'release_date_precision' => 'day',
'total_tracks' => '12',
'type' => 'album',
'uri' => 'spotify:album:6fG2eFCgUmytQWL6umtsCh',
),
'artists' =>
array (
0 =>
array (
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/artist/4ovtyvs7j1jSmwhkBGHqSr',
),
'href' => 'https://api.spotify.com/v1/artists/4ovtyvs7j1jSmwhkBGHqSr',
'id' => '4ovtyvs7j1jSmwhkBGHqSr',
'name' => 'Olamide',
'type' => 'artist',
'uri' => 'spotify:artist:4ovtyvs7j1jSmwhkBGHqSr',
),
1 =>
array (
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/artist/68R39izwNAztATrXMOqkJS',
),
'href' => 'https://api.spotify.com/v1/artists/68R39izwNAztATrXMOqkJS',
'id' => '68R39izwNAztATrXMOqkJS',
'name' => 'Bad Boy Timz',
'type' => 'artist',
'uri' => 'spotify:artist:68R39izwNAztATrXMOqkJS',
),
),
'disc_number' => '1',
'duration_ms' => '194000',
'explicit' => '1',
'external_ids' =>
array (
'isrc' => 'USUYG1330809',
),
'external_urls' =>
array (
'spotify' => 'https://open.spotify.com/track/558ULLj8yY2vT8XGtgY0q9',
),
'href' => 'https://api.spotify.com/v1/tracks/558ULLj8yY2vT8XGtgY0q9',
'id' => '558ULLj8yY2vT8XGtgY0q9
[is_local] => ',
'is_playable' => '1',
'name' => 'Loading (feat. Bad Boy Timz)',
'popularity' => '64',
'preview_url' => 'https://p.scdn.co/mp3-preview/7848a9f9ea879e6b1c6618508354e2559678aa8c?cid=756f211306ad4c9a934d07c5722790b5',
'track_number' => '10',
'type' => 'track',
'uri' => 'spotify:track:558ULLj8yY2vT8XGtgY0q9',
),
),
);
$tracks = [];
foreach ($api['tracks'] as $track) {
$_track = [
'artists' => '',
'name' => '',
'album' => '',
'image' => ''
];
$artists = [];
foreach ($track['artists'] as $artist) {
$artists[] = $artist['name'];
}
$_track['artists'] = implode(', ', $artists);
$_track['name'] = $track['name'];
$_track['album'] = $track['album']['name'];
$_track['image'] = $track['album']['images'][2]['url'];
$tracks[] = $_track;
}
var_dump($tracks);
Results in:
array(2) {
[0]=>
array(4) {
["artists"]=>
string(17) "Olamide, Omah Lay"
["name"]=>
string(25) "Infinity (feat. Omah Lay)"
["album"]=>
string(10) "Carpe Diem"
["image"]=>
string(64) "https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77"
}
[1]=>
array(4) {
["artists"]=>
string(21) "Olamide, Bad Boy Timz"
["name"]=>
string(28) "Loading (feat. Bad Boy Timz)"
["album"]=>
string(10) "Carpe Diem"
["image"]=>
string(64) "https://i.scdn.co/image/ab67616d0000485187d20b9a27d5e14d74b5cb77"
}
}
This is conveniently accomplished with array_walk_recursive. It doesn't matter at what depth or position your url key/value is, it will be found and iterated. As follows:
$urls = []; // For collecting URLs, passed in by reference:
array_walk_recursive($data, function($val, $key) use (&$urls) {
if($key === 'url') { // if $key is "url", add to $urls; ignore the rest.
$urls[] = $val;
}
});
Note that if you have multiple url keys at different depths/positions, they will all be added in. If you need to target a specific depth/position where multiple identical keys may exist, the solution would be a bit more complicated. In your use case this is not an issue, so I've kept it simple.
Also note that array_walk_recursive callback function is only called on the "leaves" or the final values of an array. As such, it will not be called on members that are arrays containing arrays. Non-final array members will simply be followed onward until "leaves" are found.

How would I return minimum value in Array of Std Class Objects (PHP)?

I have an array of StdClass Objects and want to return the "partner_code" with the minimum value for key "price". So for this example I would like to return partner_code "AC" as it is the partner with the lowest price. I tried using array_reduce, but I'm not sure what I'm doing wrong. Any help would be appreciated. Please note I am not looking to SORT this array. I just want to move the subarray containing AC (because it hold the lowest price) to the top - not sorting everything by price
Input Array:
Array
(
[0] => stdClass Object
(
[name] => Budget
[partner_code] => BU
[price] => 365.36
[tier] => 1
)
[1] => stdClass Object
(
[name] => Avis
[partner_code] => AV
[price] => 449.71
[tier] => 1
)
[2] => stdClass Object
(
[name] => E-Z
[partner_code] => EZ
[price] => 270.56
[tier] => 2
)
[3] => stdClass Object
(
[name] => Sixt
[partner_code] => SX
[price] => 280.52
[tier] => 2
)
[4] => stdClass Object
(
[name] => Alamo
[partner_code] => AL
[price] => 345.13
[tier] => 2
)
[5] => stdClass Object
(
[name] => Advantage
[partner_code] => AD
[price] => 357.61
[tier] => 2
)
[6] => stdClass Object
(
[name] => Enterprise
[partner_code] => ET
[price] => 364.46
[tier] => 2
)
[7] => stdClass Object
(
[name] => ACE
[partner_code] => AC
[price] => 186.53
[tier] => 3
)
[8] => stdClass Object
(
[name] => Fox
[partner_code] => FX
[price] => 265.25
[tier] => 3
)
[9] => stdClass Object
(
[name] => Payless
[partner_code] => ZA
[price] => 380.47
[tier] => 3
)
[10] => stdClass Object
(
[name] => Dollar
[partner_code] => ZR
[price] => 385.99
[tier] => 3
)
[11] => stdClass Object
(
[name] => Thrifty
[partner_code] => ZT
[price] => 385.99
[tier] => 3
)
[12] => stdClass Object
(
[name] => Silvercar
[partner_code] => SC
[price] => 424.10
[tier] => 3
)
[13] => stdClass Object
(
[name] => National
[partner_code] => NA
[price] => 448.82
[tier] => 3
)
[14] => stdClass Object
(
[name] => Hertz
[partner_code] => HZ
[price] => 487.33
[tier] => 3
)
)
Code:
array_reduce($this->results->companies, function($a,$b) {
echo "Prices: " . $a->price . "<br>";
return $a->price < $b->price ? (string)$a->partner_code : (string)$b->partner_code;
});
usort($array, function($a, $b) {
return ($a->price - $b->price) ;
});
echo $array[0]->partner_code;
If you really don't want to sort the array but instead use array_reduce, do so by reducing the array to one of its elements. Then you can get the partner_code property for that element.
For example
array_reduce($this->results->companies, function($lowest, $company) {
// $lowest will be null on the first iteration
return $lowest === null || $company->price < $lowest->price ?
$company : $lowest;
})->partner_code;
This method will not sort the entire array. It will pull out the subarray with the lowest price and set it as the first element. I could have nested some of the functions to make it more compact (1-liner), but that would have damaged readability.
Code: (Demo)
$results=[
(object)["name"=>"Budget","partner_code"=>"BU","price"=>"365.36","tier"=>"1"],
(object)["name"=>"Avis","partner_code"=>"AV","price"=>"449.71","tier"=>"1"],
(object)["name"=>"E-Z","partner_code"=>"EZ","price"=>"270.56","tier"=>"2"],
(object)["name"=>"Sixt","partner_code"=>"SX","price"=>"280.52","tier"=>"2"],
(object)["name"=>"Alamo","partner_code"=>"AL","price"=>"345.13","tier"=>"2"],
(object)["name"=>"Advantage","partner_code"=>"AD","price"=>"357.61","tier"=>"2"],
(object)["name"=>"Enterprise","partner_code"=>"ET","price"=>"364.46","tier"=>"2"],
(object)["name"=>"ACE","partner_code"=>"AC","price"=>"186.53","tier"=>"3"],
(object)["name"=>"Fox","partner_code"=>"FX","price"=>"265.25","tier"=>"3"],
(object)["name"=>"Payless","partner_code"=>"ZA","price"=>"380.47","tier"=>"3"],
(object)["name"=>"Dollar","partner_code"=>"ZR","price"=>"385.99","tier"=>"3"],
(object)["name"=>"Thrifty","partner_code"=>"ZT","price"=>"385.99","tier"=>"3"],
(object)["name"=>"Silvercar","partner_code"=>"SC","price"=>"424.10","tier"=>"3"],
(object)["name"=>"National","partner_code"=>"NA","price"=>"448.82","tier"=>"3"],
(object)["name"=>"Hertz","partner_code"=>"HZ","price"=>"487.33","tier"=>"3"]
];
$min=min(array_column($results,'price')); // find the minimum value
$index=array_search($min,array_column($results,'price')); // find the index of the subarray containing min value
$pulled=array_splice($results,$index,1); // extract the subarray and preserve it as $pulled
$new_results=array_merge($pulled,$results); // set $pulled as first element
var_export($new_results);
...okay, okay, here's the one-liner:
var_export(array_merge(array_splice($results,array_search(min(array_column($results,'price')),array_column($results,'price')),1),$results));
Output:
array (
0 =>
stdClass::__set_state(array(
'name' => 'ACE',
'partner_code' => 'AC',
'price' => '186.53',
'tier' => '3',
)),
1 =>
stdClass::__set_state(array(
'name' => 'Budget',
'partner_code' => 'BU',
'price' => '365.36',
'tier' => '1',
)),
2 =>
stdClass::__set_state(array(
'name' => 'Avis',
'partner_code' => 'AV',
'price' => '449.71',
'tier' => '1',
)),
3 =>
stdClass::__set_state(array(
'name' => 'E-Z',
'partner_code' => 'EZ',
'price' => '270.56',
'tier' => '2',
)),
4 =>
stdClass::__set_state(array(
'name' => 'Sixt',
'partner_code' => 'SX',
'price' => '280.52',
'tier' => '2',
)),
5 =>
stdClass::__set_state(array(
'name' => 'Alamo',
'partner_code' => 'AL',
'price' => '345.13',
'tier' => '2',
)),
6 =>
stdClass::__set_state(array(
'name' => 'Advantage',
'partner_code' => 'AD',
'price' => '357.61',
'tier' => '2',
)),
7 =>
stdClass::__set_state(array(
'name' => 'Enterprise',
'partner_code' => 'ET',
'price' => '364.46',
'tier' => '2',
)),
8 =>
stdClass::__set_state(array(
'name' => 'Fox',
'partner_code' => 'FX',
'price' => '265.25',
'tier' => '3',
)),
9 =>
stdClass::__set_state(array(
'name' => 'Payless',
'partner_code' => 'ZA',
'price' => '380.47',
'tier' => '3',
)),
10 =>
stdClass::__set_state(array(
'name' => 'Dollar',
'partner_code' => 'ZR',
'price' => '385.99',
'tier' => '3',
)),
11 =>
stdClass::__set_state(array(
'name' => 'Thrifty',
'partner_code' => 'ZT',
'price' => '385.99',
'tier' => '3',
)),
12 =>
stdClass::__set_state(array(
'name' => 'Silvercar',
'partner_code' => 'SC',
'price' => '424.10',
'tier' => '3',
)),
13 =>
stdClass::__set_state(array(
'name' => 'National',
'partner_code' => 'NA',
'price' => '448.82',
'tier' => '3',
)),
14 =>
stdClass::__set_state(array(
'name' => 'Hertz',
'partner_code' => 'HZ',
'price' => '487.33',
'tier' => '3',
)),
)
I think this is a leaner method generating the same/desired result: (Demo)
$min=$results[0]->price; // set initial/default min value
$index=0; // set initial/default index
foreach($results as $i=>$a){
if($a->price<$min){ // if a lower price...
$min=$a->price; // store new $min
$index=$i; // stor new $index
}
}
$results=array_merge($results,array_splice($results,$index,1)); // extract lowest and move to front of array
var_export($results);

How to combine two arrays of different length into an associative multidimensional array?

I have 2 arrays which are being generated by the explode() function.
This array:
Array (
[0] => uploads/str/evidence/Invoice_INVCZ769885.pdf
[1] => uploads/str/evidence/keputusantahun1.pdf
[2] => uploads/str/evidence/permohonan.pdf
[3] => uploads/str/evidence/Invoice_INVCZ7698851.pdf
[4] => uploads/str/evidence/keputusantahun11.pdf
[5] => uploads/str/evidence/permohonan1.pdf
[6] => uploads/str/evidence/Invoice_INVCZ7698852.pdf
[7] => uploads/str/evidence/Invoice_INVCZ7698853.pdf
[8] => uploads/str/evidence/keputusantahun12.pdf
)
and this array:
Array (
[0] => Invoice_INVCZ769885.pdf
[1] => Invoice_INVCZ769885.pdf
[2] => Invoice_INVCZ769885.pdf
[3] => Invoice_INVCZ769885.pdf
[4] => Invoice_INVCZ769885.pdf
[5] => Invoice_INVCZ769885.pdf
[6] => Invoice_INVCZ769885.pdf
[7] => Invoice_INVCZ769885.pdf
)
My question is how can I merge these arrays into this:
Array (
[imgs] =>
Array(
[img] => uploads/str/evidence/Invoice_INVCZ769885.pdf,
[file] => Invoice_INVCZ769885.pdf
),
[imgs] =>
Array(
[img] => uploads/str/evidence/Invoice_INVCZ769885.pdf,
[file] => Invoice_INVCZ769885.pdf
),
)
You have a mismatch in array sizes. I don't know if that was a posting error, but I have adjusted my code to suit what is literally in the question. If your arrays are actually evenly sized, then I can just rollback my edits to my first posted answer.
Input:
$a1=[
'uploads/str/evidence/Invoice_INVCZ769885.pdf',
'uploads/str/evidence/keputusantahun1.pdf',
'uploads/str/evidence/permohonan.pdf',
'uploads/str/evidence/Invoice_INVCZ7698851.pdf',
'uploads/str/evidence/keputusantahun11.pdf',
'uploads/str/evidence/permohonan1.pdf',
'uploads/str/evidence/Invoice_INVCZ7698852.pdf',
'uploads/str/evidence/Invoice_INVCZ7698853.pdf',
'uploads/str/evidence/keputusantahun12.pdf'
];
$a2=[
'Invoice_INVCZ769885.pdf',
'Invoice_INVCZ769885.pdf',
'Invoice_INVCZ769885.pdf',
'Invoice_INVCZ769885.pdf',
'Invoice_INVCZ769885.pdf',
'Invoice_INVCZ769885.pdf',
'Invoice_INVCZ769885.pdf',
'Invoice_INVCZ769885.pdf'
];
Method (Demo):
foreach($a1 as $i=>$v){
$result['imgs'][$i]['img']=$v;
if(isset($a2[$i])){$result['imgs'][$i]['file']=$a2[$i];}
}
var_export($result);
Output:
array (
'imgs' =>
array (
0 =>
array (
'img' => 'uploads/str/evidence/Invoice_INVCZ769885.pdf',
'file' => 'Invoice_INVCZ769885.pdf',
),
1 =>
array (
'img' => 'uploads/str/evidence/keputusantahun1.pdf',
'file' => 'Invoice_INVCZ769885.pdf',
),
2 =>
array (
'img' => 'uploads/str/evidence/permohonan.pdf',
'file' => 'Invoice_INVCZ769885.pdf',
),
3 =>
array (
'img' => 'uploads/str/evidence/Invoice_INVCZ7698851.pdf',
'file' => 'Invoice_INVCZ769885.pdf',
),
4 =>
array (
'img' => 'uploads/str/evidence/keputusantahun11.pdf',
'file' => 'Invoice_INVCZ769885.pdf',
),
5 =>
array (
'img' => 'uploads/str/evidence/permohonan1.pdf',
'file' => 'Invoice_INVCZ769885.pdf',
),
6 =>
array (
'img' => 'uploads/str/evidence/Invoice_INVCZ7698852.pdf',
'file' => 'Invoice_INVCZ769885.pdf',
),
7 =>
array (
'img' => 'uploads/str/evidence/Invoice_INVCZ7698853.pdf',
'file' => 'Invoice_INVCZ769885.pdf',
),
8 =>
array (
'img' => 'uploads/str/evidence/keputusantahun12.pdf',
),
),
)

Conversion of nested stdclassobject to a double dimensional array

How to convert a double std class object i.e (stdclass object having again a stdclass object in it) into an array?
I have user type; casting it only converted the single object but the object inside remained same.
Data:
Array ( [activities] => Array ( ) [goals] => stdClass Object (
[activeMinutes] => 30 [caloriesOut] => 3355
[distance] => 8.05 [steps] => 10000 )
[summary] => stdClass Object ( [activeScore] => -1 [activityCalories]
=> 1472 [caloriesBMR] => 2074 [caloriesOut] => 3308
[distances] => Array ( [0] => stdClass Object ( [activity] => total [distance] => 8.46 ) [1] => stdClass
Object ( [activity] => tracker [distance] => 8.46 )
[2] => stdClass Object (
[activity] => loggedActivities [distance] => 0 )
[3] => stdClass Object (
[activity] => veryActive [distance] => 2.35 )
[4] => stdClass Object (
[activity] => moderatelyActive [distance] => 1.63 )
[5] => stdClass Object (
[activity] => lightlyActive [distance] => 4.48 )
[6] => stdClass Object ( [activity] => sedentaryActive [distance] => 0 ) )
[fairlyActiveMinutes] => 32
[lightlyActiveMinutes] => 194 [marginalCalories] => 867 [sedentaryMinutes] =>
1125 [steps] => 11446 [veryActiveMinutes] => 31 ) )
This data has stdclass object inside an array how to convert that also into an array and make it on the whole as a double dimensional array.
I guess you have something like:
<?php
$a = [
'foo' => (object)[
'id' => 1,
'name' => 'foo',
],
'bar' => (object)[
'id' => 2,
'name' => 'bar',
'nested' => (object)['status' => 'ok'],
],
];
var_export($a);
Result:
array (
'foo' =>
stdClass::__set_state(array(
'id' => 1,
'name' => 'foo',
)),
'bar' =>
stdClass::__set_state(array(
'id' => 2,
'name' => 'bar',
'nested' =>
stdClass::__set_state(array(
'status' => 'ok',
)),
)),
)
And you try to receive something like:
array (
'foo' =>
array (
'id' => 1,
'name' => 'foo',
),
'bar' =>
array (
'id' => 2,
'name' => 'bar',
'nested' =>
array (
'status' => 'ok',
),
),
)
So the best way done it - is like mentioned #MineshPatel use next code:
var_export(json_decode(json_encode($a), true));

cakephp how to do a nested join

i have the following cakephp binding relationships set up. they are working find but how to get the comment's user record nested in the results as well?
$this->Posts->bindModel(array(
'hasOne' => array(
'User' => array(
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array('Posts.user_id = Users.id')
)
),
'hasMany' => array(
'Comment' => array(
'foreignKey' => 'post_id',
'conditions' => array('Comment.active' => 1)
)
)
));
this works great to get a result like:
[1] => Array
(
[Posts] => Array
(
[title] => test post
[body] => test body
[published] =>
[id] => 15
)
[User] => Array
(
[id] => 7
[username] => admin
[password] => d0557b9de8bb6f7fb3248a017c7b67a6
[email] => frankhinchey#gmail.com
[group_id] => 1
[created] => 2011-11-21 15:19:09
)
[Comment] => Array
(
[0] => Array
(
[id] => 10
[user_id] => 7
[post_id] => 15
[text] => testdfasdfdsfasdfasdfasdfasd
[active] => 1
[created] => 2011-12-02 20:50:57
[published] => 2011-12-05 13:58:25
)
[1] => Array
(
[id] => 11
[user_id] => 7
[post_id] => 15
[text] => this is a test comment
[active] => 1
[created] => 2011-12-02 21:31:56
[published] => 2011-12-03 11:34:32
)
)
)
)
my question is how to also get the related user on the comment? is there a way to nest in my query the hasone relationship between comment and user?
Can you use contain within bindModel? Never tried it before... but worth a shot.
$this->Posts->bindModel(array(
'hasOne' => array(
'User' => array(
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array('Posts.user_id = Users.id')
)
),
'hasMany' => array(
'Comment' => array(
'foreignKey' => 'post_id',
'conditions' => array('Comment.active' => 1),
'contain' => array('User'),
)
)
));
Actually now that I read your question I'm not really sure what you want. Do you want the user_id of who the person is commenting on? Can you just elaborate on what you want a little more.

Categories