I'm trying to figure out the best way to create an array of sets of randomly selected numbers from a range. So, for instance I have a range of numbers from which to select: 1-100. I want to generate X number of sets of 5 of those numbers WITHOUT DUPLICATES. So, I want to generate something like:
[3, 24, 32, 49, 68]
[2, 18, 43, 76, 98]
[10, 12, 23, 45, 67]
[5, 56, 64, 72, 90]
...
I know how to generate random numbers from a range once, I just am stuck on doing it X number of times without the possibility of duplicate sets.
One way to solve this problem is simply to generate the desired range $arr = range(1, 100), then shuffle it to randomize shuffle($arr) then chunk it to get sets of 5 $sets = array_chunk($arr, 5).
So the final answer:
$arr = range(1, 100); // gives us an array of 100 integers 1-100
shuffle($arr); // randomly orders those integers
$sets = array_chunk($arr, 5); // creats an array of arrays of sets of 5 integers
foreach ($sets as $set) {
echo implode(',', $set), "\n";
}
Gives you something like...
14,24,60,95,86
47,54,10,77,3
11,22,88,80,39
72,46,81,78,59
63,98,52,82,8
79,34,43,13,41
67,33,75,1,2
4,57,84,73,17
32,55,35,18,70
64,85,100,93,71
25,19,26,20,76
99,89,7,87,91
37,97,68,27,5
74,48,65,61,58
45,31,9,30,21
16,56,50,96,90
92,40,6,44,23
28,94,38,83,29
36,62,51,66,15
12,69,49,42,53
Alternative implementation
Another way to solve this problem, that doesn't require writing the entire array into memory at once and then resorting it randomly, would be to use a hashset that tracks all randomly generated integers to prevent duplicates. This gives us the same exact result except that we do both the random number generation and sorting in a single step instead of two. You can also even do the chunking in the same step this way.
Keep in mind, however, that this solution is technically slower since it's in unbounded time (we have to keep guessing if the number already exists).
I'm going to use a generator to implement this solution since it's easier to write than an Iterator pattern.
function randomizeIntegerXRangeChunked($start, $end, $chunks = 5): Generator
{
$inSet = []; // track the integers already in the set
for ($i = $start, $c = 0; $i <= $end; $i++) {
/* solution is in unbounded time */
while (isset($inSet[($n = mt_rand($start, $end))]));
$inSet[$n] = true;
yield $c => $n;
if (!($i % $chunks)) {
$c++;
}
}
}
$arr = [];
foreach (randomizeIntegerXRangeChunked(1, 100, 5) as $chunk => $int) {
$arr[$chunk][] = $int;
}
var_dump($arr);
The output will be similar to...
array(20) {
[0]=>
array(5) {
[0]=>
int(43)
[1]=>
int(52)
[2]=>
int(38)
[3]=>
int(73)
[4]=>
int(55)
}
[1]=>
array(5) {
[0]=>
int(59)
[1]=>
int(3)
[2]=>
int(71)
[3]=>
int(47)
[4]=>
int(50)
}
[2]=>
array(5) {
[0]=>
int(54)
[1]=>
int(19)
[2]=>
int(60)
[3]=>
int(40)
[4]=>
int(5)
}
[3]=>
array(5) {
[0]=>
int(26)
[1]=>
int(46)
[2]=>
int(93)
[3]=>
int(80)
[4]=>
int(63)
}
[4]=>
array(5) {
[0]=>
int(18)
[1]=>
int(45)
[2]=>
int(98)
[3]=>
int(1)
[4]=>
int(72)
}
[5]=>
array(5) {
[0]=>
int(37)
[1]=>
int(97)
[2]=>
int(15)
[3]=>
int(68)
[4]=>
int(8)
}
[6]=>
array(5) {
[0]=>
int(34)
[1]=>
int(14)
[2]=>
int(33)
[3]=>
int(24)
[4]=>
int(65)
}
[7]=>
array(5) {
[0]=>
int(4)
[1]=>
int(16)
[2]=>
int(13)
[3]=>
int(41)
[4]=>
int(86)
}
[8]=>
array(5) {
[0]=>
int(95)
[1]=>
int(12)
[2]=>
int(44)
[3]=>
int(66)
[4]=>
int(83)
}
[9]=>
array(5) {
[0]=>
int(67)
[1]=>
int(48)
[2]=>
int(91)
[3]=>
int(27)
[4]=>
int(79)
}
[10]=>
array(5) {
[0]=>
int(56)
[1]=>
int(25)
[2]=>
int(2)
[3]=>
int(64)
[4]=>
int(78)
}
[11]=>
array(5) {
[0]=>
int(57)
[1]=>
int(17)
[2]=>
int(74)
[3]=>
int(42)
[4]=>
int(69)
}
[12]=>
array(5) {
[0]=>
int(96)
[1]=>
int(20)
[2]=>
int(9)
[3]=>
int(28)
[4]=>
int(7)
}
[13]=>
array(5) {
[0]=>
int(30)
[1]=>
int(75)
[2]=>
int(21)
[3]=>
int(6)
[4]=>
int(89)
}
[14]=>
array(5) {
[0]=>
int(51)
[1]=>
int(36)
[2]=>
int(62)
[3]=>
int(58)
[4]=>
int(23)
}
[15]=>
array(5) {
[0]=>
int(85)
[1]=>
int(32)
[2]=>
int(100)
[3]=>
int(61)
[4]=>
int(49)
}
[16]=>
array(5) {
[0]=>
int(39)
[1]=>
int(87)
[2]=>
int(76)
[3]=>
int(70)
[4]=>
int(22)
}
[17]=>
array(5) {
[0]=>
int(88)
[1]=>
int(77)
[2]=>
int(10)
[3]=>
int(99)
[4]=>
int(53)
}
[18]=>
array(5) {
[0]=>
int(94)
[1]=>
int(35)
[2]=>
int(92)
[3]=>
int(90)
[4]=>
int(84)
}
[19]=>
array(5) {
[0]=>
int(81)
[1]=>
int(82)
[2]=>
int(31)
[3]=>
int(29)
[4]=>
int(11)
}
}
You can try it like this as well.
// Array
$NumberArray = array();
$TempArray = array(); // Used for storing randoms while we check via if statement.
// Loop 5 times.
for ($x=0; $x<=4; $x++) {
// Random Number 1-100
$RandomNumber = rand(1,100);
// We can use Array Push to add to the array and check using in_array.
array_push($TempArray, $RandomNumber);
// Check first thing in temp array and see if its already in number array. (If not then push)
if (!in_array(reset($TempArray), $NumberArray)) {
array_push($NumberArray, reset($TempArray)); // Add to NumberArray
// Clear Temp Array
$TempArray = array();
} else {
// Add to the loop
$x--;
}
}
print_r($NumberArray);
This outputs an array like such:
Array
(
[0] => 32
[1] => 26
[2] => 59
[3] => 96
[4] => 34
)
while developing Laravel app I stuck on one problem with array sorting. At the beginning, I used partition problem to split my array into equal chunks by time, but now I have a problem sorting by the id of the array.
My array:
array(2) {
[0]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(4) <----- this is value i need to sort by
[1]=>
int(15)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(6)
[1]=>
int(10)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(2)
[1]=>
int(8)
[2]=>
string(85) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
[1]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(3)
[1]=>
int(14)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(5)
[1]=>
int(12)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(1)
[1]=>
int(7)
[2]=>
string(55) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
}
I need to sort by the first value of each array, so called my id.
Output I need:
array(2) {
[0]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(1)
[1]=>
int(15)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(2)
[1]=>
int(10)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(3)
[1]=>
int(8)
[2]=>
string(85) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
[1]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(4)
[1]=>
int(14)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(5)
[1]=>
int(12)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(6)
[1]=>
int(7)
[2]=>
string(55) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
}
maybe any of you guys have any solutions how i could solve this problem, so far i tried a lot of sorting functions, none of them seems to work properly for me.
Thank you!
this may not be the best way to do this, but you can try something like this.
$tempArray = array();
$sortedArray = array();
foreach ($yourArray as $key1 => $arr){// create 2D array
foreach ($arr as $key2 => $arr2){
$tempArray[] = $arr2;
}
}
foreach ($tempArray as $keyTemp => $tempElem){
$sortingArray[] = $tempElem[0];//get array value you need to sort
}
asort($sortingArray);// sort array maintaining the key
foreach ($sortingArray as $keySort => $sortElem){//loop through sorted array to push elements to a new array
$sortedArray[] = $tempArray[$keySort];
}
print_r($sortedArray);
Ok, So I am working with the reddit api using the php sdk wrapper by #jcleblanc ( https://github.com/jcleblanc/reddit-php-sdk )
That information is not necessarily relevant to the question but I want to give you a frame of reference so you understand what I am looking to do.
The api returns what I believe is a mixed json object as a response when I make calls to reddit. The response length varies and is not consistent. The only this that is consistent is if the action was successful, it returns the key pair
["success"]=> bool(true)
I want to basically search whatever is returned by the api for the key "success" and I can simply evaluate for true or false. Can anybody help with some code to do that?
Here is a sample complete return:
object(stdClass)#1171 (2) { ["jquery"]=> array(29) { [0]=> array(4) { [0]=> int(0) [1]=> int(1) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(4) "body" } } [1]=> array(4) { [0]=> int(1) [1]=> int(2) [2]=> string(4) "attr" [3]=> string(4) "find" } [2]=> array(4) { [0]=> int(2) [1]=> int(3) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(7) ".status" } } [3]=> array(4) { [0]=> int(3) [1]=> int(4) [2]=> string(4) "attr" [3]=> string(4) "hide" } [4]=> array(4) { [0]=> int(4) [1]=> int(5) [2]=> string(4) "call" [3]=> array(0) { } } [5]=> array(4) { [0]=> int(5) [1]=> int(6) [2]=> string(4) "attr" [3]=> string(4) "html" } [6]=> array(4) { [0]=> int(6) [1]=> int(7) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [7]=> array(4) { [0]=> int(7) [1]=> int(8) [2]=> string(4) "attr" [3]=> string(3) "end" } [8]=> array(4) { [0]=> int(8) [1]=> int(9) [2]=> string(4) "call" [3]=> array(0) { } } [9]=> array(4) { [0]=> int(1) [1]=> int(10) [2]=> string(4) "attr" [3]=> string(8) "redirect" } [10]=> array(4) { [0]=> int(10) [1]=> int(11) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(66) "https://www.reddit.com/r/mysubreddit/comments/12hyas2/my_no_link_post/" } } [11]=> array(4) { [0]=> int(1) [1]=> int(12) [2]=> string(4) "attr" [3]=> string(4) "find" } [12]=> array(4) { [0]=> int(12) [1]=> int(13) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(11) "*[name=url]" } } [13]=> array(4) { [0]=> int(13) [1]=> int(14) [2]=> string(4) "attr" [3]=> string(3) "val" } [14]=> array(4) { [0]=> int(14) [1]=> int(15) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [15]=> array(4) { [0]=> int(15) [1]=> int(16) [2]=> string(4) "attr" [3]=> string(3) "end" } [16]=> array(4) { [0]=> int(16) [1]=> int(17) [2]=> string(4) "call" [3]=> array(0) { } } [17]=> array(4) { [0]=> int(1) [1]=> int(18) [2]=> string(4) "attr" [3]=> string(4) "find" } [18]=> array(4) { [0]=> int(18) [1]=> int(19) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(12) "*[name=text]" } } [19]=> array(4) { [0]=> int(19) [1]=> int(20) [2]=> string(4) "attr" [3]=> string(3) "val" } [20]=> array(4) { [0]=> int(20) [1]=> int(21) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [21]=> array(4) { [0]=> int(21) [1]=> int(22) [2]=> string(4) "attr" [3]=> string(3) "end" } [22]=> array(4) { [0]=> int(22) [1]=> int(23) [2]=> string(4) "call" [3]=> array(0) { } } [23]=> array(4) { [0]=> int(1) [1]=> int(24) [2]=> string(4) "attr" [3]=> string(4) "find" } [24]=> array(4) { [0]=> int(24) [1]=> int(25) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(13) "*[name=title]" } } [25]=> array(4) { [0]=> int(25) [1]=> int(26) [2]=> string(4) "attr" [3]=> string(3) "val" } [26]=> array(4) { [0]=> int(26) [1]=> int(27) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(1) " " } } [27]=> array(4) { [0]=> int(27) [1]=> int(28) [2]=> string(4) "attr" [3]=> string(3) "end" } [28]=> array(4) { [0]=> int(28) [1]=> int(29) [2]=> string(4) "call" [3]=> array(0) { } } } ["success"]=> bool(true) }
The code you gave mean you were calling json_decode($response). This return an object.
It should be:
$resArray = json_decode($response, true)
Which return an associated array.
Then to use:
if (isset($resArray['success'])) {
//your logic here
}
Your object has two properties: jquery and success.
All you need to get the success property is this:
$success = $your_object->success;
As long as the returned object has a success property, that's all you should need to do, regardless of how many other properties it has, or the size of any of its other properties.
If you aren't sure if the object will have the success property, you can use
$success = !empty($your_object->success);
which will evaluate to true if the property exists and is true, and false otherwise.
You might be able to just throw it into
json_decode
If it complains about the format you can do some direct string manipulation beforehand and then you'll be able to reference it directly as an associative array.
Use var_dump to get familiar with the structure after you get a successful parse.
I'm using tmdb.org's API to retrieve movie data. Their credits are returned as a separate response, though. I want to be able to display only the Director and the first 10 cast members. How would I manipulated that data in the a JSON response?
Here is an excerpt of what is returned. I removed a lot of the credits so that it's not so long.
array(26) {
["adult"]=>
bool(false)
["backdrop_path"]=>
string(32) "/4iJfYYoQzZcONB9hNzg0J0wWyPH.jpg"
["belongs_to_collection"]=>
array(4) {
["id"]=>
int(10)
["name"]=>
string(20) "Star Wars Collection"
["poster_path"]=>
string(31) "/ghd5zOQnDaDW1mxO7R5fXXpZMu.jpg"
["backdrop_path"]=>
string(32) "/d8duYyyC9J5T825Hg7grmaabfxQ.jpg"
}
["budget"]=>
int(11000000)
["genres"]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(12)
["name"]=>
string(9) "Adventure"
}
[1]=>
array(2) {
["id"]=>
int(28)
["name"]=>
string(6) "Action"
}
[2]=>
array(2) {
["id"]=>
int(878)
["name"]=>
string(15) "Science Fiction"
}
}
["homepage"]=>
string(61) "http://www.starwars.com/films/star-wars-episode-iv-a-new-hope"
["id"]=>
int(11)
["imdb_id"]=>
string(9) "tt0076759"
["original_language"]=>
string(2) "en"
["original_title"]=>
string(9) "Star Wars"
["overview"]=>
string(312) "Princess Leia is captured and held hostage by the evil Imperial forces in their effort to take over the galactic Empire. Venturesome Luke Skywalker and dashing captain Han Solo team together with the loveable robot duo R2-D2 and C-3PO to rescue the beautiful princess and restore peace and justice in the Empire."
["popularity"]=>
float(9.414021)
["poster_path"]=>
string(32) "/tvSlBzAdRE29bZe5yYWrJ2ds137.jpg"
["production_companies"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
string(9) "Lucasfilm"
["id"]=>
int(1)
}
[1]=>
array(2) {
["name"]=>
string(38) "Twentieth Century Fox Film Corporation"
["id"]=>
int(306)
}
}
["production_countries"]=>
array(1) {
[0]=>
array(2) {
["iso_3166_1"]=>
string(2) "US"
["name"]=>
string(24) "United States of America"
}
}
["release_date"]=>
string(10) "1977-03-20"
["revenue"]=>
int(775398007)
["runtime"]=>
int(121)
["spoken_languages"]=>
array(1) {
[0]=>
array(2) {
["iso_639_1"]=>
string(2) "en"
["name"]=>
string(7) "English"
}
}
["status"]=>
string(8) "Released"
["tagline"]=>
string(44) "A long time ago in a galaxy far, far away..."
["title"]=>
string(9) "Star Wars"
["video"]=>
bool(false)
["vote_average"]=>
float(7.9)
["vote_count"]=>
int(4584)
["credits"]=>
array(2) {
["cast"]=>
array(106) {
[0]=>
array(7) {
["cast_id"]=>
int(3)
["character"]=>
string(14) "Luke Skywalker"
["credit_id"]=>
string(24) "52fe420dc3a36847f8000441"
["id"]=>
int(2)
["name"]=>
string(11) "Mark Hamill"
["order"]=>
int(0)
["profile_path"]=>
string(32) "/ws544EgE5POxGJqq9LUfhnDrHtV.jpg"
}
[1]=>
array(7) {
["cast_id"]=>
int(4)
["character"]=>
string(8) "Han Solo"
["credit_id"]=>
string(24) "52fe420dc3a36847f8000445"
["id"]=>
int(3)
["name"]=>
string(13) "Harrison Ford"
["order"]=>
int(1)
["profile_path"]=>
string(32) "/aVKNqtkzZCymupOrvwxozamRyVc.jpg"
}
[2]=>
array(7) {
["cast_id"]=>
int(5)
["character"]=>
string(20) "Princess Leia Organa"
["credit_id"]=>
string(24) "52fe420dc3a36847f8000449"
["id"]=>
int(4)
["name"]=>
string(13) "Carrie Fisher"
["order"]=>
int(2)
["profile_path"]=>
string(32) "/pbleNurCYdrLFQMEnlQB2nkOR1O.jpg"
}
[3]=>
array(7) {
["cast_id"]=>
int(6)
["character"]=>
string(17) "Grand Moff Tarkin"
["credit_id"]=>
string(24) "52fe420dc3a36847f800044d"
["id"]=>
int(5)
["name"]=>
string(13) "Peter Cushing"
["order"]=>
int(3)
["profile_path"]=>
string(32) "/fg7ufC0IMr6VasQzzdmTtX5ycQF.jpg"
}
[4]=>
array(7) {
["cast_id"]=>
int(14)
["character"]=>
string(20) "Obi-Wan "Ben" Kenobi"
["credit_id"]=>
string(24) "52fe420dc3a36847f8000477"
["id"]=>
int(12248)
["name"]=>
string(13) "Alec Guinness"
["order"]=>
int(4)
["profile_path"]=>
string(32) "/89QNJ7u1hNaCglkotmZEbwCNp1M.jpg"
}
[5]=>
array(7) {
["cast_id"]=>
int(7)
["character"]=>
string(20) "See Threepio (C-3PO)"
["credit_id"]=>
string(24) "52fe420dc3a36847f8000451"
["id"]=>
int(6)
["name"]=>
string(15) "Anthony Daniels"
["order"]=>
int(5)
["profile_path"]=>
string(32) "/cljvryjb3VwTsNR7fjQKjNPMaBB.jpg"
}
[6]=>
array(7) {
["cast_id"]=>
int(8)
["character"]=>
string(19) "Artoo-Detoo (R2-D2)"
["credit_id"]=>
string(24) "52fe420dc3a36847f8000455"
["id"]=>
int(130)
["name"]=>
string(11) "Kenny Baker"
["order"]=>
int(6)
["profile_path"]=>
string(32) "/sdd9rgifNF9C51RejG7sUGU8Bka.jpg"
}
[7]=>
array(7) {
["cast_id"]=>
int(15)
["character"]=>
string(9) "Chewbacca"
["credit_id"]=>
string(24) "52fe420dc3a36847f800047b"
["id"]=>
int(24343)
["name"]=>
string(12) "Peter Mayhew"
["order"]=>
int(7)
["profile_path"]=>
string(32) "/din1s5H4C4CfcnkHfEeRcdFlsVj.jpg"
}
[8]=>
array(7) {
["cast_id"]=>
int(16)
["character"]=>
string(11) "Darth Vader"
["credit_id"]=>
string(24) "52fe420dc3a36847f800047f"
["id"]=>
int(24342)
["name"]=>
string(12) "David Prowse"
["order"]=>
int(8)
["profile_path"]=>
string(32) "/cJtmBVrjYwawh2cCiAfZkEjPeqc.jpg"
}
[9]=>
array(7) {
["cast_id"]=>
int(17)
["character"]=>
string(28) "Voice of Darth Vader (voice)"
["credit_id"]=>
string(24) "52fe420dc3a36847f8000483"
["id"]=>
int(15152)
["name"]=>
string(16) "James Earl Jones"
["order"]=>
int(9)
["profile_path"]=>
string(32) "/2ZuBf3ip2RXhkiQqGUjbUzAf4Nx.jpg"
}
[10]=>
array(7) {
["cast_id"]=>
int(18)
["character"]=>
string(10) "Uncle Owen"
["credit_id"]=>
string(24) "52fe420dc3a36847f8000487"
["id"]=>
int(33032)
["name"]=>
string(10) "Phil Brown"
["order"]=>
int(10)
["profile_path"]=>
string(32) "/exkyN66HiZWJDmpcOza2hWoswOo.jpg"
}
[11]=>
array(7) {
["cast_id"]=>
int(19)
["character"]=>
string(9) "Aunt Beru"
["credit_id"]=>
string(24) "52fe420dc3a36847f800048b"
["id"]=>
int(131625)
["name"]=>
string(14) "Shelagh Fraser"
["order"]=>
int(11)
["profile_path"]=>
string(32) "/xNfiibBvknHztEnL0g7dcdrxOKq.jpg"
}
[12]=>
array(7) {
["cast_id"]=>
int(24)
["character"]=>
string(10) "Chief Jawa"
["credit_id"]=>
string(24) "52fe420dc3a36847f800049f"
["id"]=>
int(132538)
["name"]=>
string(11) "Jack Purvis"
["order"]=>
int(12)
["profile_path"]=>
string(32) "/tuFTY1jhlEgZm3vM80KdAEvHwNI.jpg"
}
int(1)
["job"]=>
string(8) "Director"
["name"]=>
string(12) "George Lucas"
["profile_path"]=>
string(32) "/8qxin8urtFE0NqaZNFWOuV537bH.jpg"
}
[1]=>
array(6) {
["credit_id"]=>
string(24) "562e75309251414006009955"
["department"]=>
string(7) "Writing"
["id"]=>
int(1)
["job"]=>
string(6) "Writer"
["name"]=>
string(12) "George Lucas"
["profile_path"]=>
string(32) "/8qxin8urtFE0NqaZNFWOuV537bH.jpg"
}
So, from the above, I want to able to display the data in the main array and then for the appended data, I'd like it to display like:
Director: George Lucas
Cast: Carrie Fisher, Mark Hammill, Harrison Ford [and then 7 more]
I'm new to fetching API data. So any help on this would be much appreciated.
Why list() method using two array (array of arrays) as values don't work (for one specific array ($subCategories) the list wrap in another array, the other array is output equals before list()).
list method:
list($subCategories, $subCategoriesName) = [$subCategories, $subCategoriesName]; //[array, array]
[$subCategories, $subCategoriesName] are the return of a function
$subCategories before list
array(4) {
[0]=>
array(3) {
["id"]=>
int(1)
["sub_category_name"]=>
string(14) "Industrilizado"
["category_id"]=>
int(1)
}
[1]=>
array(3) {
["id"]=>
int(2)
["sub_category_name"]=>
string(9) "In natura"
["category_id"]=>
int(1)
}
[2]=>
array(3) {
["id"]=>
int(3)
["sub_category_name"]=>
string(13) "Comida pronta"
["category_id"]=>
int(1)
}
[3]=>
array(3) {
["id"]=>
int(4)
["sub_category_name"]=>
string(11) "Desidratado"
["category_id"]=>
int(1)
}
}
$subCategories after list
array(1) {
[0]=>
array(4) {
[0]=>
array(3) {
["id"]=>
int(1)
["sub_category_name"]=>
string(14) "Industrilizado"
["category_id"]=>
int(1)
}
[1]=>
array(3) {
["id"]=>
int(2)
["sub_category_name"]=>
string(9) "In natura"
["category_id"]=>
int(1)
}
[2]=>
array(3) {
["id"]=>
int(3)
["sub_category_name"]=>
string(13) "Comida pronta"
["category_id"]=>
int(1)
}
[3]=>
array(3) {
["id"]=>
int(4)
["sub_category_name"]=>
string(11) "Desidratado"
["category_id"]=>
int(1)
}
}