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

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);

Related

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)

Pulling sub arrays out of larger array [closed]

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'];
}

converting array of objects to simple array [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 8 years ago.
Improve this question
I have an array of objects, each object consists of an id and an organisation. It looks like this
Array (
[0] => stdClass Object (
[id] => 2 [organisation] => org1
)
[1] => stdClass Object (
[id] => 4 [organisation] => org2
)
[2] => stdClass Object (
[id] => 1 [organisation] => org3
)
)
I need to convert it into a simple associative array ([id]=>organisation,...) so the above example would look like this
Array (
[2] => org1
[4] => org2
[1] => org3
)
Greatful for any thoughts
Loop through it using a foreach statement and append it to another array.
$finished = [];
foreach($array as $arr) {
$finished[$arr->id] = $arr->organisation;
}
$result = array();
foreach($array as $arr) {
$result[$arr->id] = $arr->organisation;
}
echo "<pre>";print_r($result);

Parsing a String of Zip Codes with 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
I am moving my company from a legacy service to a new system. I have written some conversion utilities in PHP to facilitate the move. One issue I am coming accross is converting the zip codes.
In the old system, Zips could be mixed in one line:
700,701,30518,30511,30000-30010,400,30097,30101-30201
In my new system, each type of zip must be broken into it's own line:
700,701,400
30518,30511,30097
30000-30010,30101-30201
So I need to import the first code block and output the second code block to 3 variables. I am working in PHP. Any Guidance? The order of the zips is random. I'm Lost.
Well, this is what I got:
/(\d+)(-\d+)?/s
/s is to ignore newlines.
<?php
$subject = "700,701,400\n30518,30511,30097\n30000-30010,30101-30201\n";
preg_match_all("/(\d+)(-\d+)?/s", $subject, $matches);
print_r($matches[0]);
?>
Here's output:
Array
(
[0] => 700
[1] => 701
[2] => 400
[3] => 30518
[4] => 30511
[5] => 30097
[6] => 30000-30010
[7] => 30101-30201
)
Alternatively, you can do this:
$arr = explode("\n", $subject);
for ($i = 0; $i < count($arr); $i++) {
preg_match_all("/(\d+)(-\d+)?/s", $arr[$i], $matches);
print_r($matches[0]);
}
If there's only three types, then you can just preg_match for each line.
Output:
Array
(
[0] => 700
[1] => 701
[2] => 400
)
Array
(
[0] => 30518
[1] => 30511
[2] => 30097
)
Array
(
[0] => 30000-30010
[1] => 30101-30201
)
Array
(
)

how to print values if we have an assoc array [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
I am working in codeigniter.
Structure of my array is
Array
(
[0] => stdClass Object
(
[0] => 15
[1] => 14
[2] => 0
[3] => 0
[4] => 0
)
[1] => stdClass Object
(
[0] => 15
[1] => 14
[2] => 0
[3] => 0
[4] => 0
)
)
I want to print the value of arrays.
As soon as they are not arrays but objects, you need to use
$arr[0]->{0}
form.
Use foreach for access all
foreach ($array as $values)
{
foreach ($values as $value)
{
echo $value;
}
}
Or direct access:
$array[0]->{1};
$array[0]->{2};
$array[1]->{5};
etc..
Usage:
$array[ARRAY INDEX]->{OBJECT INDEX};
Suppose your array is in the variable $arr. Then use it:
$i = 0;
foreach($arr as $array){
echo $array[$i];
$i++;
}
OR you can do this:
echo $arr[1]->0; //and so on
You can get data using
$arr[0]->{0}
OR
$arr[0]->{'0'}
stdClass is php's generic empty class, kind of like Object in Java or object in Python. It is useful for anonymous objects, dynamic properties, etc.
So its not array its an object. you will have to use object operator (->)
check below post
stdClass Object foreach PHP

Categories