PHP - Order info from 2 arrays - php

Ok, I have 2 arrays. 1 array has the ORDER of the information that should be displayed, and the other array has the information. But I am having trouble trying to determine a way to display the information in the order it is supposed to be displayed in...
For example, I have this array here, OF WHICH I HAVE NO CONTROL OVER (meaning the array gets done prior to my code even running, so I have to START with this array of which I can NOT alter this actual array. I could build an array from this array if it would help any? But not sure how...?)
$attach_info(0,1,5,4);
The numerical values here point to actual information to be displayed...
0 = Members name ($attachments['member']['link'])
1 = Filename ($attachments['file']['link'])
2 = Filesize ($attachments['file']['filesize'])
3 = Downloads ($attachments['file']['downloads'])
4 = Link to post ($attachments['topic']['link'])
5 = Time of attachment ($attachments['topic']['time'])
6 = Image to display ($attachments['file']['image']['link'])
The other array gets populated within a function and get RETURNED from that function with all of the information to be displayed...
The returned information is this array:
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$attachments[$row['id_attach']] = array(
'member' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '' . $row['poster_name'] . '',
),
'file' => array(
'filename' => $filename,
'filesize' => round($row['filesize'] /1024, 2) . $txt['kilobyte'],
'downloads' => $row['downloads'],
'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
'link' => '<img src="' . $settings['images_url'] . '/icons/clip.gif" alt="" /> ' . $filename . '',
'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
'image' = array(
'id' => $id_thumb,
'width' => $row['width'],
'height' => $row['height'],
'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '" />',
'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" />',
'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
'link' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" />',
),
),
'topic' => array(
'id' => $row['id_topic'],
'subject' => $row['subject'],
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
'link' => '' . $row['subject'] . '',
'time' => timeformat($row['poster_time']),
),
)
}
AND this is just an example really, cause this array gets built within that function for EACH attachment, which goes into another array. Ofcourse there is MORE info. here than I need, but I will shrink it down later.
So, currently I am doing a foreach on the $attachments array to grab all of the attachments, and this gives me all of the information for the attachments as well. No problem with any of that.
The problem I am having is how to grab the information from the $attachments array in the order from the $attach_info array. I need the output to be what is in the attach_info array number-wise, in the order it is in.
For example, if $attach_info array is like this:
$attach_info(0,1,5,4);
Than it should output the Members name first, than the Filename, than the Time of the Attachment, and lastly a link to the post.
So the order of this array is important. And should grab this information from the $attachments array.
Can someone please help me with a way to set this up within a foreach loop so that the information gets echo'd in the correct order. And the attach_info array can change ofcourse. I'm looking for a fast approach to doing this. I'd rather not have to determine the order of the information and what information should be displayed everytime for each loop within the $attachments array if you know what I mean.
Thanks you guys Rock :)
EDIT
The $attachments array listed above is only 1 output of the actual array. It is actually outputting ALL attachments, so I need it to be able to LOOP through all attachments and still output the correct order and the correct information ONLY! Sorry if I failed to explain this.
$attachments array is 1 loop within an foreach... so it is just the output of only 1 foreach. I need them all.
So, what I can do now is this...
foreach($attachments as $item)
{
echo
$item['member']['link'] . $item['file']['link'] . $item['file']['filesize'] . $item['file']['downloads'] . $item['topic']['link'] . $item['topic']['time'] . $item['file']['image']['link'];
}
BUT I need to echo the items in the correct order according to $attach_info array, and only echo those items!

best of all try this:
foreach($attach_info as $val){
switch($val){
case 0: echo $attachments['member']['link'];
break;
case 1: echo $attachments['file']['link'];
break;
case 2: echo $attachments['file']['filesize'];
break;
case 3: echo $attachments['file']['downloads'];
break;
case 4: echo $attachments['topic']['link'];
break;
case 5: echo $attachments['topic']['time'];
break;
case 6: echo $attachments['file']['image']['link'];
break;
}
}

May be it helps http://php.net/manual/ru/function.usort.php

Related

Shortcode parameter not working in WordPress

I am trying to pass amount parameters in url from the shortcode. The from and to attributes are working fine but the amount attribute is not working. The output shows the value of 1.
using the shortcode like this:
[exchange_rate from="USD" to="EUR" amount="100"]
function exchange_rate_shortcode($atts) {
$atts = shortcode_atts(array(
'from' => 'AED',
'to' => 'NPR',
'amount' => '1',
), $atts);
$url = "https://api.fastforex.io/convert?from=" . $atts['from'] . "&to=" . $atts['to'] . "&amount=" . $atts['amount'] . "&api_key=xxxx-xxxxx-xxxx";
$result = file_get_contents($url);
$result = json_decode($result, true);
return number_format((float)$result['result']['rate'], 2, '.', '') . " " . $atts['to'];
}
add_shortcode('exchange_rate', 'exchange_rate_shortcode');
Perhaps you somehow wrote the word "amount" wrong and therefore an error occurs. To avoid confusion, use the build_query function.
Your code will look like this:htt
function exchange_rate_shortcode($atts)
{
$atts = shortcode_atts(array(
'from' => 'AED',
'to' => 'NPR',
'amount' => '1',
'api_key' => 'xxxx-xxxxx-xxxx',
), $atts);
// to make sure the correct value type is specified
$atts['amount'] = (float)$atts['amount'];
$url = "https://api.fastforex.io/convert?" . build_query($atts);
$result = file_get_contents($url);
if ( ! empty($result)) {
$result = json_decode($result, true);
return number_format((float)$result['result']['rate'], 2, '.', '') . " " . $atts['to'];
}
return '';
}
add_shortcode('exchange_rate', 'exchange_rate_shortcode');

How to fix string request/get function

I am trying to pull url string items (utm fields), combine them into one variable (vtid) and pass to page but it is only passing dashes, the url info is not passing.
<?
$utm_source = $_REQUEST['utm_source'];
$utm_medium = $_REQUEST['utm_medium'];
$utm_campaign = $_REQUEST['utm_campaign'];
$utm_term = $_REQUEST['utm_term'];
$utm_content = $_REQUEST['utm_content'];
$vtid = $utm_source . "-" . $utm_medium . "-" . $utm_campaign . "-" . $utm_term . "-" . $utm_content;
?>
print_r($_REQUEST); shows
Array (
[gr_subscriber] => exists
[email] => myemail#me.com
[http_referer] => example.com=&utm_source=as&utm_medium=ms&utm_campaiā€¦
[webforms_id] => 657433
[name] => Friend
)

Cut sequence after reaching a key in an array

Let's imagine I ordered my friends in a list, each of them with certain score, depending on our friendship status. My point is showing the top 3 of them, cutting the sequence with a break. However, my wish is to make id dynamic. What can be placed in "Rene" text to make the php dynamic?
<?php
$friends = array('Julio' => 9, 'Sergio' => 4, 'Jorge' =>7, 'Rene' => 8, 'Marvin' => 6, 'Brayan' => 5, 'Edvin' => 10, 'Delfido' => 3, 'Jhon' => 2);
arsort($friends);
foreach ($friends as $friend => $score){
echo $friend . ' ' . $score . '<br>';
if($friend == '**Rene**'{
break;
}
}
?>
First you have to order the array on value (and maintain the key)
arsort($friends);
Source: http://php.net/manual/en/array.sorting.php
Then count the loop and stop it when it hits 3:
$count = 1;
foreach ($friends as $friend => $score){
echo $friend . ' ' . $score . '<br>';
$count++;
if($count>=3){
break;
}
}

PHP Insert Values if they are set

Looking for a little guidance as I don't know how to echo all my set options into an asana request. So I have an array from a user submitted form:
Array
(
[_wpcf7] => 5
[_wpcf7_version] => 4.1.2
[_wpcf7_locale] => en_US
[_wpcf7_unit_tag] => wpcf7-f5-o1
[_wpnonce] => 1531937d4e
[your-name] => TEST
[your-email] => test#test.com
[title] => test
[Description] => test
[venues] => Dragonfly
[duedate] => 2015-04-11
[options] => Array
(
[0] => Print Business Card (3.5x2)
[1] => Google Ads Package
[2] => Print Ticket (1.5x5.5)
[3] => Print Magazine Ad (8x10.75)
)
[_wpcf7_is_ajax_call] => 1
[upload] => ditch_fridays_logo.png
)
From there I parse the array to grab the variables:
$submittedBy = $posted_data['your-name'];
$eMail = $posted_data['your-email'];
$projectTitle = $posted_data['title'];
$projectDescription = $posted_data['Description'];
$venue = $posted_data['venues'];
$deadline = $posted_data['duedate'];
$options = $posted_data['options'];
$upload = $posted_data['upload'];
And then I create the asana task
// First we create the task
$result = $asana->createTask(array(
'workspace' => $workspaceId, // Workspace ID
'name' => $projectTitle, // Name of task
'assignee' => 'somewhere#somewhere.com', // Assign task to...
'due_on' => $deadline,
'notes' => 'Submitted By: ' . $submittedBy . "\n" . 'E-Mail: ' . $eMail . "\n\n" . '--------------------------------------------------' . "\n\n" . 'Task Name: ' . $projectTitle . "\n\n" . 'Description: ' . $projectDescription . "\n\n" . 'Venue: ' . $venue . "\n\n" . 'Deadline: ' . $deadline . "\n\n" . 'Attachments: http://inkentertainment.com/graphics/saved/' . $upload
));
The problem is, I cant include the selected options in the notes part of the task creation. How would I go about including all the options ($posted_data['options']) only if the user has selected them? (on the form there is a list of about 10, but the users will select 2 or 3)
I hope I have been clear enough for you to understand, if not let me know and I will try and clarify
$options = ( isset ( $posted_data[ 'options' ] ) &&
is_array( $posted_data[ 'options' ] ) ) ?
$posted_data[ 'options' ] : array();
here you'll have an array with zero or more elements.
If you need a string you may then use implode
http://php.net/manual/en/function.implode.php
ex: $options = implode( ', ', $options );

renaming files in an array incrementally

I've got a folder full of files pulled into an array:
Array
(
[2] => 2_1_page2-img5.jpg
[3] => 2_2_page2-img5-big.jpg
[4] => 3_1_page2-img6.jpg
[5] => 3_2_page2-img6-big.jpg
[6] => 4_1_page2-img3.jpg
[7] => 4_2_page2-img3-big.jpg
[8] => 6_2_page2-img3-big.jpg
[9] => 8_2_page2-img3-big.jpg
)
I'm trying to create a script that will rename them all so they start at 1 and rename incrementally. The ideal solution would be:
Array
(
[2] => 1_1_page2-img5.jpg
[3] => 1_2_page2-img5-big.jpg
[4] => 2_1_page2-img6.jpg
[5] => 2_2_page2-img6-big.jpg
[6] => 3_1_page2-img3.jpg
[7] => 3_2_page2-img3-big.jpg
[8] => 4_2_page2-img3-big.jpg
[9] => 5_2_page2-img3-big.jpg
)
I won't know whether each number will have one or two images attached to it.
So far I've got:
$filecounter = 1;
$previousNumber = $filecounter;
foreach($listing as $imagename){
// $imagename: the original filename
// $prefixNumber: the first number from the filename
// $reducedfilename: the filename without the prefix
// $previousNumber: the previous number
// $filecounter: the count up from 1
$prefixNumber = stristr($imagename, '_',true);
$reducedfilename = ltrim(stristr($imagename, '_'), '_');
if ($prefixNumber != $filecounter && $prefixNumber != $previousNumber){
$previousNumber = $filecounter;
$filecounter++;
echo "filecounter: $filecounter<br>prefixNumber: $prefixNumber<br>previousNumber: $previousNumber<br>";
echo "1. Renaming: " .$imagename . " to " . $filecounter . "_" . $reducedfilename . "<br><br>";
//rename($directory . $imagename, $directory . $filecounter. "_" . $reducedfilename);
} else {
echo "filecounter: $filecounter<br>prefixNumber: $prefixNumber<br>previousNumber: $previousNumber<br>";
echo "2. Renaming: " .$imagename . " to " . $previousNumber . "_" . $reducedfilename . "<br><br>";
//rename($directory . $imagename, $directory . $previousNumber . "_" . $reducedfilename);
}
I've given myself a ton of visual pointers to try and help but I think I've been staring at it too long and I honestly can't work it out.
Does anyone please have any ideas what I'm doing wrong here?
I think something like this should probably work for you.
$filecounter = 1;
$currentPrefix = false;
$lastPrefix = false;
foreach ($listing as $imagename) {
$aFileParts = explode('_', $imagename);
$currentPrefix = $aFileParts[0];
if (!$lastPrefix) {
$lastPrefix = $currentPrefix;
}
if ($currentPrefix != $lastPrefix) {
$filecounter++;
$lastPrefix = $currentPrefix;
}
$newFileName = $filecounter . '_' . $aFileParts[1] . '_' . $aFileParts[2];
}

Categories