Pulling sub arrays out of larger array [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I bought a script known as CoinDice, and I need to take the output array and create a PHP code that will pull everyone of these values: EX. [0],[1],[3],[4] and so on. In the arrays I would like to capture the amount, address, txid, category, confirmations.
Array
(
[transactions] => Array
(
[0] => Array
(
[amount] => 8.131E-5
[blockhash] => 00000000000000001bb164ff85fe78f5f54bde00d4462d8bef19f96421334673
[address] => 1HJqsE5CoPnVSPvQVKU4oPCVku47eu39kr
[fee] => 0.0001
[txid] => 75c9ad12fdbe957ad3c98edfc02fa212dde0ca8e941373c82df5c545599936ce
[label] =>
[time] => 1415703105
[category] => receive
[confirmations] => -394
[blockindex] => 490989
[account] =>
)
[1] => Array
(
[amount] => 1.5E-5
[blockhash] => 000000000000000001fd1bc6e7add41bbca9e4d77a43ccd430a144c70c08d985
[address] => 16GttENVpxmq1oZjnP5TkLWDrwi3HBa1iu
[fee] => 0.0001
[txid] => 29420d9a380df6546920a7cd5da5f9750219ef8e1102f2194547a38323e6dfd7
[label] =>
[time] => 1415453123
[category] => receive
[confirmations] => -815
[blockindex] => 489453
[account] =>
)

All you do is a simple foreach loop like so:
foreach($array['transactions'] as $key => $object) {
// The $key would be the [0], [1], [2], etc...
// Here I am just echoing the values to the page,
// but you can do your code stuff here.
echo $object['amount'];
echo $object['blockhash'];
echo $object['address'];
// etc...
}
The foreach will run down your array() and apply your code to each sub-array.
To access the values directly:
echo $array['transactions'][0]['amount'];
echo $array['transactions'][0]['blockhash'];
//...etc.
echo $array['transactions'][1]['amount'];
echo $array['transactions'][1]['blockhash'];
//...etc.
Hopefully this ends your 3-week long headache.

Something like.
$yourArray = ....
foreach($yourArray['transactions'] as $values){
echo $values;
}

I assume that you only have access to the output as shown in your question, not the actual array as PHP code.
See here for a function that will parse print_r() style output back into an array. I've tested it on a trivial array example, so it might work in your case.

as far as i understood your question is that you want to fetch your data under "transactions" array,
suppose you have array name sample,
so you can simply use foreach loop lik
foreach($sample['transactions'] as $transaction)
{
echo $transaction['amount'];
}

Related

How to generate report like pivot table from php 3d array on question_id base [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Actually I am generating a report and print out answers under that question id. Like 13377 questiin id answered 3 times so i want to echo their answers underneath and the same for other question ids. All data is dynamic. Get unique questions id that will be table head and all answers of that id will be underneath. Its like pivot table report. So I am not getting the logic how to do that.
Array
(
[0] => Array
(
[question_id] => 13377
[add_more_counter] => 0
[front_title] => Text question
[value] => first time fill
)
[1] => Array
(
[question_id] => 13377
[add_more_counter] => 0
[front_title] => Text question
[value] => second time fill
)
[2] => Array
(
[question_id] => 13377
[add_more_counter] => 0
[front_title] => Text question
[value] => text question 1
)
[3] => Array
(
[question_id] => 13378
[add_more_counter] => 0
[front_title] => Text box 2
[value] => text question 2
)
)
and i want to transform into this
<table border="1"><tbody><tr><th>13377</th><th>13378</th></tr><tr><td>firt time fill</td><td></td></tr><tr><td>second time fill</td><td></td></tr><tr><td>text question 1</td><td></td></tr><tr><td></td><td>text question 2</td></tr></tbody></table>
please help. Thanks in advance.
You need to iterate over keys keeping index constant,
$a = []// Your array
// considering all array have same keys
$keys = array_keys($a[0]);
echo "<table>";
foreach ($keys as $value) {
echo "<tr>";
foreach ($a as $k => $v) {
echo "<td>".$a[$k][$value]."</td>";
}
echo "</tr>";
}
echo "</table>";

Parse JSON with PHP - irregular format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
New to php and trying to figure out how to parse API data which is returned in what looks like a weird format. Here is a sample of the data:
[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],...,"zierfischforum.at":["1","0","0","0.00"]}]
Here is an example of how you can parse your JSON as an array:
$json_string = '[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],"zierfischforum.at":["1","0","0","0.00"]}]';
$json_array = json_decode($json_string, true); // true gets us an array
echo '<pre>';
print_r($json_array);
echo $json_array[1]['video2.stack.com'][0];
Provides the following results:
Array
(
[0] => Array
(
[campaign_id] => 9000
[date] => 2016-01-11
[totalcount] => 1838
[page] => 1
[totalpages] => 1
[index] => 1
[count] => 1838
)
[1] => Array
(
[video2.stack.com] => Array
(
[0] => 84254
[1] => 105
[2] => 0
[3] => 83.71
)
[zierfischforum.at] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0.00
)
)
)
84254
First we output the entire array. Based on data there we are able to single out a value for one of the array parts for video2.stack.com. It is relatively easy to traverse and you should be able extract any information you need. You could even build a recursive search function for your JSON.
NOTE: I removed some of your data(the part ,...) as it made your JSON non-valid.

PHP Multi-dimensional array rearranging [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following array that i want to re-arrange
Array
(
[0] => stdClass Object
(
[feeds_id] => 1338
[flag] => 0
)
[1] => stdClass Object
(
[feeds_id] => 1339
[flag] => 0
)
[2] => stdClass Object
(
[feeds_id] => 1339
[flag] => 1
)
)
I want to arrange it to look like this
[1338] => Array (
[0] => 0
)
[1339] => Array (
[0] => 0
[1] => 1
)
This code should work:
$newArray=array();
foreach($items as $item){
if(!is_array($newArray[$item->feeds_id])){
$newArray[$item->feeds_id]=array();
}
array_push($newArray[$item->feeds_id],$item->flag);
}
You should first create an empty array where the new data will be stored. Then, inside the foreach, you should use array_push, BUT if the sub-array in where you want to put the data is not an array, you should declare it first (that's why the "if" before the array_push)

PHP splitting a URL, delimitted by a '=' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Attempting to split the values after &file= for the following example URL. So i'm trying to extract the number at the end. Any suggestions?
http://blablablabla.com/test.php?type=mp4&tv=&file=3797
you can do it with many ways like using parse_url()
$query = parse_url('http://blablablabla.com/test.php?type=mp4&tv=&file=3797', PHP_URL_QUERY);
parse_str($query, $values);
print_r($values);
output will be
(
[type] => mp4
[tv] =>
[file] => 3797
)
you can also do it with using regex like below
$str="http://blablablabla.com/test.php?type=mp4&tv=&file=3797";
preg_match_all('/([^?&=#]+)=([^&#]*)/',$str,$matched);
print_r($matched);
output will be
(
[0] => Array
(
[0] => type=mp4
[1] => tv=
[2] => file=3797
)
[1] => Array
(
[0] => type
[1] => tv
[2] => file
)
[2] => Array
(
[0] => mp4
[1] =>
[2] => 3797
)
)
You can do this along with the sanitize.
$file=filter_input(INPUT_GET, "file", FILTER_SANITIZE_STRING) // This will get the data and clean the data too.
If you don't need the sanitize, you can simply use it as,
$file=$_GET['file']
I personally recommend the first method.

How to compare arrays and objects to find a match PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My first question is, how do I search values of my array inside the objects array [words] and return object if match is found?
How do I search for a better match? In the example below, second object is a better match with 2 words in common, rather than first with only 1 match.
Array
(
[0] => blue
[1] => green
[2] => love
[3] => sandro
)
stdClass Object
(
[1] => stdClass Object
(
[words] => Array
(
[0] => green
[1] => blue
)
[html] => html+img+link+code
)
[2] => stdClass Object
(
[words] => Array
(
[0] => love
[1] => sex
[2] => blue
)
[html] => html+img+link+code
)
)
Code I tried:
foreach ($ads_arr as $ad) {
print_r(array_intersect($ad->words,$words_arr));
}
You can use a forloop for your case, but you should consider defining real php class (not stdClass ) and implement some methods to help you.
foreach($main_std as $id => $sub_std){
$count_match[$id] = 0;
// now, check for each objects
// you can use an other loop with in_array, array_intersect
// or any other way
foreach($the_array as $word_search)
{
// for each word you're looking for, add +1
if (in_array($word_search, $sub_std->words))
$count_match[$id] ++;
}
}
// here, $count_match is an array you can sort by best match or whatever you want
Try out array_intersect() :
$output = array_intersect($array1, $array2);
print_r($output);

Categories