0

I am new to using multidimensional arrays with php, I have tried to stay away from them because they confused me, but now the time has come that I put them to good use. I have been trying to understand how they work and I am just not getting it.

What I am trying to do is populate results based on a string compare function, once I find some match to an 'item name', I would like the first slot to contain the 'item name', then I would like to increment the priority slot by 1.

So when when I'm all done populating my array, it is going to have a variety of different company names, each with their respective priority...

I am having trouble understanding how to declare and manipulate the following array:

$matches = array(
    'name'=>array('somename'),
    'priority'=>array($priority_level++)
);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231
  • 3
    Your description doesn't sound like a multi-dimensional array. It sounds like a single-dimensional array of key-value pairs. A multi-dimensional array is an array of arrays. Unless your "priority" value is an array, you don't need multi-dimensional. – David Jul 12 '12 at 18:46

6 Answers6

1

Multi-dimensional arrays are easy. All they are is an array, where the elements are other arrays.

So, you could have 2 separate arrays:

$name = array('somename');
$priority = array(1);

Or you can have an array that has these 2 arrays as elements:

$matches = array(
    'name' => array('somename'),
    'priority' => array(1)
);

So, using $matches['name'] would be the same as using $name, they are both arrays, just stored differently.

echo $name[0]; //'somename';
echo $matches['name'][0]; //'somename';

So, to add another name to the $matches array, you can do this:

$matches['name'][] = 'Another Name';
$matches['priority'][] = 2;

print_r($matches); would output:

Array
(
    [name] => Array
        (
            [0] => somename
            [1] => Another Name
        )

    [priority] => Array
        (
            [0] => 1
            [1] => 2
        )

)
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

So, in what you have, your variable $matches will point to a keyed array, the 'name' element of that array will be an indexed array with 1 entry 'somename', there will be a 'priority' entry with a value which is an indexed array with one entry = $priority_level.

I think, instead what you probably want is something like:

$matches[] = array(name => 'somename', $priority => $priority_level++);

That way, $matches is an indexed array, where each index holds a keyed array, so you could address them as:

$matches[0]['name'] and $matches[0]['priority'], which is more logical for most people.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Robert
  • 2,441
  • 21
  • 12
  • 1
    This makes sense! So, everytime I do $matches[ ]['name'] = 'somename'; it will store it in the 'next' slot of the array? Also in your declaration above, don't you mean for name to be 'name'? – AnchovyLegend Jul 12 '12 at 18:55
1

In my opinion, an easier structure to work with would be something more like this one:

$matches = array(
    array( 'name' => 'somename', 'priority' => $priority_level_for_this_match ),
    array( 'name' => 'someothername', 'priority' => $priority_level_for_that_match )
)

To fill this array, start by making an empty one:

$matches = array();

Then, find all of your matches.

$match = array( 'name' => 'somename', 'priority' => $some_priority );

To add that array to your matches, just slap it on the end:

$matches[] = $match;

Once it's filled, you can easily iterate over it:

foreach($matches as $k => $v) {
    // The value in this case is also an array, and can be indexed as such
    echo( $v['name'] . ': ' . $v['priority'] . '<br>' );
}

You can also sort the matched arrays according to the priority:

function cmp($a, $b) {
    if($a['priority'] == $b['priority'])
        return 0;
    return ($a['priority'] < $b['priority']) ? -1 : 1;
}
usort($matches, 'cmp');

(Sourced from this answer)

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
1

In this case, could this be also a solution with a single dimensional array?

$matches    = array(
        'company_1' => 0,
        'company_2' => 0,
);

if (isset($matches['company_1'])) {
    ++$matches['company_1'];
} else {
    $matches['company_1'] = 1;
}

It looks up whether the name is already in the list. If not, it sets an array_key for this value. If it finds an already existing value, it just raises the "priority".

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
0
$matches['name'][0]   --> 'somename'
$matches['priority'][0]  ---> the incremented $priority_level value
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • isn't this an example that uses two different arrays? – AnchovyLegend Jul 12 '12 at 18:50
  • PHP's multi-dim arrays really just multidimensional in name only. They're truly just arrays of arrays of arrays of .... Think of each nesting level as a sub-directory in a file system, and each of those "directories" is specified via the `[...]` notation. What i've put above is what you'd using in PHP to access the values you saved in your child arrays in your example. – Marc B Jul 12 '12 at 18:51
0

Like David said in the comments on the question, it sounds like you're not using the right tool for the job. Try:

$priorities = array();
foreach($companies as $company) {
    if (!isset($priorities[$company])) { $priorities[$company] = 0; }
    $priorities[$company]++;
}

Then you can access the priorities by checking $priorities['SomeCompanyName'];.

WWW
  • 9,734
  • 1
  • 29
  • 33