php reorder array based on order of other array - php

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.

Related

How to Combine Array String As Keys And Number As Value In Php

I have a problem to do the combining between 2 arrays.
In the first array as a string (example array data):
Array
(
[0] => courses
[1] => courses
[2] => courses
[3] => courses
[4] => courses
)
Second array as a value (Example Array values):
Array
(
[0] => 64
[1] => 63
[2] => 62
[3] => 2
[4] => 9
)
After I tried to combine between 2 arrays like this:
$combine = (array_combine($data, $courses))
I can only get a combination result like this:
Array
(
[courses] => 9
)
How can i get the result of combination between the two arrays above, like this?
Array
(
[courses] => 64
[courses] => 63
[courses] => 62
[courses] => 2
[courses] => 9
)
Thanks In Advance.
You are not able to generate your intended output as you can't have multiple of the same key in an array. This is why you are ending up with one entry of 'courses'.
For example this would be do-able:
Array
(
[courses] => array(
[0] => 63
[1] => 63
[2] => 62
[3] => 2
[4] => 9
)
)
You need to change your approach.

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.

Merge array by value of a value cell (no doubles)

I was wondering if it's possible to merge an array by a value of a cell (no doubles) right of the php box.
To clarify this is my current array :
Array
(
[0] => Array
(
[Score] => 90
[Name] => David,
)
[1] => Array
(
[Score] => 90
[Name] => Eric,
)
[2] => Array
(
[Score] => 77
[Name] => Eva,
)
[3] => Array
(
[Score] => 77
[Name] => Gila,
)
)
So we have:
2 persons (David and Eric) that have the same score (90)
2 persons (Eva and Gila) that have the same score (77)
So by the end of the day (in that case) I want to get only 2 rows (merged by score) and the names will be added to the same line that merged by score.
This is what I'm expecting to get :
Array
(
[0] => Array
(
[Score] => 90
[Name] => David,Eric,
)
[1] => Array
(
[Score] => 77
[Name] => Eva,Gila,
)
)
Here is my php code :
<?php
header("Content-Type:text/plain");
$myArray=array();
$myArray[]=array('Score'=>'90','Name'=>'David,');
$myArray[]=array('Score'=>'90','Name'=>'Eric,');
$myArray[]=array('Score'=>'77','Name'=>'Eva,');
$myArray[]=array('Score'=>'77','Name'=>'Gila,');
print_r($myArray);
Thanks!

Working with multi-level arrays

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

CakePHP Model index array needed, but can't generate from for loop counter. Need model indexed array for this to work

I am working on a function that submits multiple records on various relationship types. The main issue I am running into is the format of the array. In order for my saveAll() to work on my multiple relationships setup, the array needs to be in this format as you can see the models are Keys (first array below).
My main question is: 1) Is it possible to strip the numerical indexes off the second layer of the second array below?
I am returning my input fields like so. You can see the prefixed counter (which I believe is what is creating the numeric index on that second level).
<?php echo $this->Form->input("$i.monthly_cost", array('label' => 'Monthly Cost')); ?>
I am using a for loop counter for the fields. So my question number to is: can this for value be changed to something that will work with Cake's saveAll()?
<?php for ($i = 1; $i <= 2; $i++) { ?>
Example where models are the keys (this is the format I need):
Array
(
[User] => Array
(
[username] => billy
)
[Profile] => Array
(
[sex] => Male
[occupation] => Programmer
)
The only output I can get on my multiple input array (below is the debug() dump)
My actual output is numerically indexed:
Array
(
[Plan] => Array
(
[1] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 14
[applicant_id] => 1
[state_id] => 1
)
[2] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 2
[applicant_id] => 4
[state_id] => 1
)
)
[1] => Array
(
[1] => Array
(
[Zip] => Array
(
[0] => 487
[1] => 486
[2] => 485
[3] => 484
[4] => 483
)
)
)
[2] => Array
(
[2] => Array
(
[Zip] => Array
(
[0] => 485
[1] => 484
[2] => 483
)
)
)
)
Did you already check out the Set Core Utility Library? This can help you out a lot with array management.

Categories