select one array out of multiples API spits out - php

I never encountered something similar:
I'm using an API that is spitting out exactly this text
(I'm using print_r($content) where content is the result form API)
Request:
Array
(
[0] => GET
[1] => https://api.xxx.com/api/v1/xxx
[2] => Array
(
)
[3] =>
)
Request:
Array
(
[0] => GET
[1] => https://api.xxx.com/api/v1/xxx
[2] => Array
(
)
[3] =>
)
Array
(
[symbol] => XXX
[timestamp] => 1514994289069
[datetime] => 2018-01-03T15:44:49.069+00:00
etc...
how to I get the third array?
$content[2] clearly doesn't do the desired output...

Related

PHP Array delete an entire array by one element value

So this is how my Array ($dataArray) looks oks like:
Array
(
[0] => Array
(
[0] => Date
[1] => Time
[2] => Duration
[3] => Info
[4] => Client
)
[1] => Array
(
[0] => 2021-12-01
[1] => 10:45:43
[2] => 237
[3] => Some text from
[4] => Client 1
)
[2] => Array
(
[0] => 2021-12-01
[1] => 11:29:13
[2] => 77
[3] => Nothing important
[4] => Client 2
)
[3] => Array
(
[0] => 2021-12-01
[1] => 11:53:03
[2] => 44
[3] => anonymous
[4] => Client 1
)
I need to Loop trough it to search the Client Names, and if i find the matching name in the Element 4 then delete the entire Array.
$ExportKDname = "Client 1"
foreach($dataArray as $key => $sub_array) {
if($sub_array[4] == $ExportKDname) {
unset($dataArray[$key]);
break;
}
}
print_r($dataArray);
But with this code none of the arrays will be deleted. And I just can not find the right way to do it.
The Final array what I need to look like if we find the "Client 1" in the array would be like this:
Array
(
[0] => Array
(
[0] => Date
[1] => Time
[2] => Duration
[3] => Info
[4] => Client
)
[1] => Array
(
[0] => 2021-12-01
[1] => 11:29:13
[2] => 77
[3] => Nothing important
[4] => Client 2
)
In the if condition you are saying "if u match with $sub_arr[4] == $ExportKDname unset it and stop the loop". the machine doing that. when it matched first time it removes and stoping. If u wanna delete all match do not write break; let it continue. So delete or make it comment break; line.
You can array_filter your variable and check if value is in_array.
With PHP 7.4+ syntax it should look like this:
$result = array_filter($dataArray, fn ($innerArray) => !in_array('Client 1', $innerArray));

PHP - Re-arrange multi-array so that certain combinations have the same key(index)

I have an array, which looks like this:
Array
(
[sku_0017768] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017766] => Array
(
[0] => ff5a47bd699bcb52944d0727a5b443b6
[1] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017723] => Array
(
[3] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_0017767] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
)
[sku_8402350] => Array
(
[0] => a3c83832b4089d83d164b3cac596fc7e
[1] => fb91d14d405be52ce989acd2918ea1a0
[2] => ff5a47bd699bcb52944d0727a5b443b6
[3] => d14afce16e477663ebe51fdd65cd5010
)
[sku_8402200] => Array
(
[0] => d14afce16e477663ebe51fdd65cd5010
)
)
Now, this array represents a list of SKU's with one or more 'combination codes'. So, in this example 'sku_0017768' matches product sku_0017766 because they share the same combination code.
sku_8402350 has multiple combination as you can see.
What I need to have now is a new array, where the index(key) of the combination code needs to be same among the other products within the combination.
Output should look like this:
Array
(
[sku_0017768] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017766] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
[1] => ff5a47bd699bcb52944d0727a5b443b6
)
[sku_0017723] => Array
(
[0] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_0017767] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
)
[sku_8402350] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
[1] => ff5a47bd699bcb52944d0727a5b443b6
[2] => d14afce16e477663ebe51fdd65cd5010
[3] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_8402200] => Array
(
[2] => d14afce16e477663ebe51fdd65cd5010
)
)
The difficulty I have is that when a key is already in use, the key of the other products within the combination should increase (with 1) as well, until there is an empty key which the combination can use.
Can anybody help me pointing me in the right direction? Feel free to ask if anything is not clear.

Merge 2 arrays when keys of 1 matches value in 2

I need help figuring out what I am doing wrong and/or if there is a better way to do it, my question is two part. The first part is I am using Joomla 3.5.1 and in the template I have two fieldsets, one that allows the user to enter links to their social media accounts and the second I am attempting to make a sort if you will using a set of drop downs. This may or may not be the best so I am open to options here.
Now for the second part. I am able to bring in the both field sets into separate arrays, for simplicity array 1 and array 2. Array 1 has the links and array 2 has the order. So my question is this, how would be the best way to loop through and match everything up while removing the empties in array 1 and "none" in array 2.
One thought I did have is should array 2 not be multidimensional and let the key be the "soXYZ", but then how would the best way to match it up knowing that the lengths are different and the order as well.
Array 2 where (-1) are "None" in the drop downs
Array
(
[0] => soPhone
[1] => soContact
[2] => soFacebook
[3] => soMap
[4] => -1
[5] => -1
[6] => -1
[7] => -1
[8] => -1
[9] => -1
[10] => -1
[11] => -1
[12] => -1
[13] => -1
[14] => -1
)
Array 1
Array
(
[0] => Array
(
[0] => soPhone
[1] => 555.867.5309
)
[1] => Array
(
[0] => soContact
[1] => Contact
)
[2] => Array
(
[0] => soMap
[1] => Map
)
[3] => Array
(
[0] => soFacebook
[1] => Facebook
)
[4] => Array
(
[0] => soTwitter
[1] => Twitter
)
[5] => Array
(
[0] => soGoogle
[1] => Google Plus
)
[6] => Array
(
[0] => soLinkedIn
[1] => Linked In
)
[7] => Array
(
[0] => soPinterest
[1] => Pinerest
)
[8] => Array
(
[0] => soYouTube
[1] => YouTube
)
[9] => Array
(
[0] => soVimeo
[1] => Vimeo
)
[10] => Array
(
[0] => soYelp
[1] => Yelp
)
[11] => Array
(
[0] => soInstagram
[1] => Instagram
)
[12] => Array
(
[0] => soTripAdvisor
[1] => Trip Advisor
)
[13] => Array
(
[0] => soHouzz
[1] => Houzz
)
[14] => Array
(
[0] => soAngiesList
[1] => Angies List
)
)
Array Result
Array
(
[0] => Array
(
[0] => soPhone
[1] => 555.867.5309
)
[1] => Array
(
[0] => soContact
[1] => Contact
)
[2] => Array
(
[0] => soFacebook
[1] => Facebook
)
[3] => Array
(
[0] => soMap
[1] => Map
)
)
Any guidance is appreciated.
After the power of posting I was able to eventually get my desired result, which as follows:
I set up three arrays arrLinks (originally Array 1) , arrLinksOrder (originally Array 2) and arrLinksCombined (Array Result), then populated arrLinks with the user entered text links on the template, arrLinksOrder pulls from the drop downs.
My original plan was to have arrLinks be multidimensional but in the end I just set the key's for each in my code.
I then used this stackoverflow link to help combine the arrLinksOrder and arrLinks. Once everything is combined I was able to build the html and really clean up the file to something easier to manage.
Code below:
// Populate the contents of the text-box into an array
$arrLinks = array(
"soFacebook"=> $tbFacebook,
"soTwitter"=> $tbTwitter,
"soGoogle"=> $tbGoogle,
"soLinkedIn"=> $tbLinkedIn,
"soPinterest"=> $tbPinterest,
"soYouTube"=> $tbYouTube,
"soInstagram"=> $tbInstagram,
);
// Populate an array (arrLinksOrder) with the drop down selections
for ($i=1; $i < ($linksCount+1); $i++) {
$varName = 'listSocialOrder'.$i;
$arrLinksOrder[$i] = $$varName;
}
// Combine arrLinksOrder and arrLinks, if the arrLinksOrder exists in arrLinks
foreach($arrLinksOrder as $key) {
if(array_key_exists($key, $arrLinks)) {
$arrLinksCombined[$key] = $arrLinks[$key];
}
}
If anyone has any improvements please feel free to share, this is what worked for me at the time.

Cakephp retrieving data from database

I'm having trouble modeling my database. Currently it looks like this (I'm hiding the irrelevant fields): http://i.imgur.com/SF9FzaD.png
It is working fine, for example, when I want it to return a list of all the servers:
$this->Server->find('all');
It returns an array with the right information:
Array
(
[0] => Array
(
[Server] => Array (...)
[User] => Array (...)
[Highlight] => Array
(
[0] => Array
(
[id] => 39
[id_server] => 8
[id_highlight] => 1
)
[1] => Array
(
[id] => 40
[id_server] => 8
[id_highlight] => 5
)
)
[SubServer] => Array(...)
)
[1] => Array
(
[Server] => Array (...)
[User] => Array (...)
[Highlight] => Array
(
[0] => Array
(
[id] => 41
[id_server] => 10
[id_highlight] => 4
)
[1] => Array
(
[id] => 42
[id_server] => 10
[id_highlight] => 5
)
)
[SubServer] => Array(...)
)
)
In short, each game "server" has some kind of higlights pointed by the user. Like "Anti-cheat system", "Active staff", "Custom events", etc. Each of these highlights have an id and a name.
Is there a way to grab the name from the table highlight_names corresponding to each of the highlights.id_hightlight (and the rest of the data, like the array above) using Model::find()?

Select every first array value in associative array

I dynamically create this associative file array through a PHP script, and now I am wondering how do I select all values inside the root array?
I'll explain here in code.
This is the array that is currently getting generated everytime:
Array
(
[HTML] => Array
(
[0] => Index.php
)
[Javascript] => Array
(
[0] => Javascript.js
[1] => Jquery.js
)
[0] => New Text Document.txt
[Scripts] => Array
(
[0] => Get_Server_files.Script.php
)
[Style] => Array
(
[0] => General.css
[1] => Menu.css
[2] => Style.css
)
[images] => Array
(
[WelcomeImages] => Array
(
[0] => WelcomeImage0.png
[1] => WelcomeImage1.png
[2] => WelcomeImage2.png
[3] => WelcomeImage3.png
[4] => WelcomeImage4.png
[5] => WelcomeImage5.png
[6] => WelcomeImage6.png
)
[0] => bg.jpg
)
)
From here, how do I select every first value? As in selecting
HTML
Javascript
Scripts
Style
Images.
And not anything that is inside these arrays.
I have searched but everytime it's not quite what I would like.
You can use array_keys() to get the names and then use array_filter() to remove the 0 values:
$names = array_filter(array_keys($data));
print_r($names);
Output:
Array
(
[0] => HTML
[1] => Javascript
[3] => Scripts
[4] => Style
[5] => images
)
Demo.
Use array_keys()
$keys = array_keys($array);

Categories