Working with multi-level arrays - php

hello i am learning PHP and came upon this multi-level array after using print_r on $this->root
Array (
[0] => 9
[obj] => 3562
[gen] => 0
[1] => Array (
[0] => 5
[1] => Array (
[/AcroForm] => Array (
[0] => 8
[1] => 3563
[2] => 0
)
[/Metadata] => Array (
[0] => 8
[1] => 3559
[2] => 0
)
[/PageLabels] => Array (
[0] => 8
[1] => 3389
[2] => 0
)
[/Pages] => Array (
[0] => 8
[1] => 3392
[2] => 0
)
[/Type] => Array (
[0] => 2
[1] => /Catalog
)
)
)
) Array (
[0] => 9
[obj] => 8
[gen] => 0
[1] => Array (
[0] => 5
[1] => Array (
[/Type] => Array (
[0] => 2
[1] => /Catalog
)
[/Pages] => Array (
[0] => 8
[1] => 1
[2] => 0
)
[/OpenAction] => Array (
[0] => 6
[1] => Array (
[0] => Array (
[0] => 8
[1] => 3
[2] => 0
)
[1] => Array (
[0] => 2
[1] => /FitH
)
[2] => Array (
[0] => 0
)
)
)
[/PageLayout] => Array (
[0] => 2
[1] => /OneColumn
)
)
)
)
i have an question about the behavior of using multi-level arrays, i want to use this function
$pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
and $this->root[1][1]['/Pages'] which i believe is used to check the array for these keys and if it exists then use as variable for pdf_resolve_object
so my question is 2-fold, one is does $this->root[1][1]['/Pages'] check the array and goes through the keys? if not what is its behavior? and 2 when it checks the array does it go through just the top 4 keys or all of the sub-keys?
If someone can help or link me to some learning material that would be much appreciated, thank you!

1) It does not check for the presence of the array keys -- rather it assumes that those keys already exist and passes the value into the function. If any of the keys did not exist, PHP would issue an E_NOTICE to the effect of Notice: Undefined index: that the key was not found. To check for them would require a call to isset() or array_key_exists() like:
if (isset($this->root[1][1]['/Pages'])) {
$pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
}
2) There is no need for it to iterate through to find the keys. Knowing the array keys already means they can be accessed directly without iteration. In memory, PHP has stored the array keys and the memory locations of the values they point to. Therefore, with the key alone, PHP can return the value without needing to traverse the array.
There is a lot of good information in the PHP manaul on Arrays

Related

PHP - How to upload image in a multidimensional array loop

I am not able to upload image, I tried using the below code to display. But nothing gets displayed.
PHP Code is as
$question_image = $allquestion["'ques_image'"];<br>
$imagetemp = $_FILES[$question_image]['name'];<br>
echo "IMAGE NAME".$imagetemp;
And Returned Array
Array
(
[1] => Array
(
['ques_title'] => Question 1
['ques_image'] => galaxy_s7_black.png
['choice'] => Array
(
[0] => Array
(
[0] => Question 1 - First Choice
[1] => 127
)
[1] => Array
(
[0] => Question 1 - second Choice
[1] => 128
)
)
)
[2] => Array
(
['ques_title'] => Question 2
['ques_image'] => iphone7_rosegold.png
['choice'] => Array
(
[0] => Array
(
[0] => Question 2 - First Choice
[1] => 131
)
[1] => Array
(
[0] => Question 2 - second Choice
[1] => 132
)
)
)
)
How to upload image in a multidimensional array loop.
How did you print the array?
You must call the array arguments in step-by-step
for example :
echo $_FILES[1]['ques_image']
or
echo $_FILES[2]['ques_image']

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.

PHP Count number of Arrays

Simple question that I am struggling to find answer for. I have an array as follows:
Array (
[0] => Array ( [0] => 2014-05-14 02:11:16 [1] => 1 )
[1] => Array ( [0] => 2014-05-19 05:05:17 [1] => 76 )
[2] => Array ( [0] => 2014-05-20 00:28:41 [1] => 35 )
[3] => Array ( [0] => 2014-05-21 01:24:01 [1] => 25 )
)
All I need to do is count how many Arrays there are.
The answer based on the above would be 4 (0,1,2&3).
I am positive this is a very simple thing but I cannot fathom how - any and all suggestions welcomed.
Using count() should work for you :
$num = count($array);

php reorder array based on order of other array

Given this array:
Array
(
[0] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[1] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[2] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
[3] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
)
The ssm_featured_post_id value corresponds to the value of the array items in the second array.
I want to order the first array items in the same order as the items in the second array
Array
(
[1] => 63
[0] => 70
[3] => 1
[2] => 49
)
so the result after sorting would be
Array
(
[0] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[1] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[2] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
[3] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
)
The simpler way would be to use usort and write a function that uses the second table to compare two values from first table.
You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.

Categories